Knowledge Visualization, Geographical Knowledge
A knowledge visualization tutorial on easy methods to construct three completely different maps in Python Altair: choropleth map, dot density map, and proportional image map
For information scientists, visualizing information is an important talent. It helps to rapidly perceive patterns and correlations within the information that might in any other case be missed. Geographical maps are an effective way to visualise spatial information and can be utilized to discover tendencies in numerous nations or areas.
This text will present you easy methods to construct a geographical map utilizing the Python Altair library.
The article will cowl the next:
Downloading the mapChoropleth mapDot density mapProportional image map
To create a geographical map in Python Altair, you want a topoJSON file specifying the geographic boundaries of the areas on the map and a dataset with values for every area.
Creating the topoJSON file is past the scope of this text, however there are lots of assets already obtainable on the internet that present ready-to-use maps, resembling:
Upon getting your topoJSON file, you possibly can load it into Altair utilizing the topo_feature() perform:
import altair as alt
url = # url to a topoJSON filefeature = # function title to extract
data_map = alt.topo_feature(url, function)
The topo_feature() perform receives as enter the URL to the topoJSON file and the function to extract. The function is the string worth similar to a toddler of the objects attribute within the topoJSON object. To retrieve the function string , open the topoJSON object to seek for the objects attribute.
A choropleth map is a geographical map the place areas are coloured in line with a metric. For instance, a choropleth map of the US might present which states have the very best inhabitants density.
Contemplate the Life expectancy at beginning dataset launched by OECD Knowledge as an open information dataset [1]. Use the next code to attract a choropleth map in Altair exhibiting the life expectancy in Europe in 2010:
import pandas as pdimport altair as alt
alt.data_transformers.disable_max_rows()
df = pd.read_csv(‘information/life_expectancy_at_birth.csv’)df = df[df[‘Region’] == ‘Europe & Central Asia’]df = df[df[‘Year’] == 2010]
url = “https://uncooked.githubusercontent.com/deldersveld/topojson/grasp/continents/europe.json”
data_map = alt.topo_feature(url, “continent_Europe_subunits”)
alt.Chart(data_map).mark_geoshape().encode(tooltip=’properties.geounit:N’,colour=alt.Colour(‘Life Expectancy:Q’)).undertaking(‘mercator’).properties(width=500,peak=300).transform_lookup(lookup=’properties.geounit’,from_=alt.LookupData(df, ‘Nation’, [‘Country’, ‘Life Expectancy’]))
Use the transform_lookup() perform to merge the data_map dataset and the life expectancy dataset. The earlier code builds the next map:
A dot density map is a geographical map the place every entity is represented by a dot. For instance, a dot density map might present all of the nationwide parks in Europe. A dot would signify every park.
Contemplate the airports dataset, launched as open information by OurAirports.com. Learn extra concerning the dataset license right here. Use the next code to attract a dot density map in Altair:
import pandas as pdimport altair as altalt.data_transformers.disable_max_rows()
df = pd.read_csv(‘information/airports.csv’)df = df[df.type == ‘small_airport’]
url = ” = alt.topo_feature(url, “continent”)
base = alt.Chart(supply).mark_geoshape(fill=’lightgray’,stroke=’white’).undertaking(‘mercator’).properties(width=800,peak=600)
factors = alt.Chart(df).mark_circle().encode(longitude=’longitude_deg:Q’,latitude=’latitude_deg:Q’,dimension=alt.worth(10),tooltip=’title:N’,colour=alt.worth(‘purple’))
(base + factors).properties(title=’Small airports on the planet’).configure_title(fontSize=20)
Because of this, you’ll receive the next map:
A proportional image map is a dot density map the place the dimensions of the dot will depend on a sure variable. For instance, you possibly can signify the variety of airports for every nation on the planet as a variable-size dot. The larger the dot representing a rustic, the larger the variety of airports in that nation.
Contemplate once more the airports dataset, launched as open information by OurAirports.com. Use the next code to attract a proportional image map in Altair:
import pandas as pdimport altair as altalt.data_transformers.disable_max_rows()
url = ” = alt.topo_feature(url, “continent”)
base = alt.Chart(supply).mark_geoshape(fill=’lightgray’,stroke=’white’).undertaking(‘mercator’).properties(width=800,peak=600)
df = pd.read_csv(‘information/airports.csv’)df = df[df.type == ‘large_airport’]df2 = df.groupby(by=[‘iso_country’])[‘name’].depend().to_frame().reset_index()df2.rename(columns={‘title’ : ‘number_of_airports’},inplace=True)
factors = alt.Chart(df).mark_circle().encode(longitude=’longitude_deg:Q’,latitude=’latitude_deg:Q’,dimension=alt.Measurement(‘number_of_airports:Q’, title=’# airports’),colour=alt.worth(‘purple’)).properties(width=500,peak=400).transform_lookup(lookup=’iso_country’,from_=alt.LookupData(df2, ‘iso_country’, [‘iso_country’, ‘number_of_airports’]))
base + factors
Please discover that we use group by to group nations to calculate the variety of airports for every nation.
The next determine exhibits the ensuing chart:
Congratulations! You will have simply realized easy methods to construct a geographical map in Altair, utilizing three alternative ways: a choropleth map, a dot density map, and a proportional image map.
Altair is a really helpful library for information visualization. For instance, you need to use Altair to attract a time sequence. Learn this text to study 3 methods that you could be not know to visualise a time sequence in Altair.
Additionally, you need to use Altair to declutter a chart and rework it right into a extra readable chart. Learn this text to learn to declutter a bar chart in Altair.
When you have come this far to learn, it’s already so much for at the moment. Thanks! You’ll be able to learn extra about me on this article.
If you happen to like information visualization as I do, take into account exploring information storytelling. Knowledge storytelling is the artwork of telling tales by means of information. If you realize sufficient about information visualization however wish to transfer to information storytelling, learn the ebook #Makeover Monday by A. Kriebel and Eva Murray. Learn a private overview of the ebook right here.
[1] OECD (2023), Life expectancy at beginning (indicator). doi: 10.1787/27e0fc9d-en (Accessed on 18 January 2023)