Techno Blender
Digitally Yours.

3 Ways to Build a Geographical Map in Python Altair | by Angelica Lo Duca | Jan, 2023

0 159


Data Visualization, Geographical Data

A data visualization tutorial on how to build three different maps in Python Altair: choropleth map, dot density map, and proportional symbol map

Photo by Kyle Glenn on Unsplash

For data scientists, visualizing data is an essential skill. It helps to quickly understand patterns and correlations in the data that would otherwise be missed. Geographical maps are a great way to visualize spatial data and can be used to explore trends in different countries or regions.

This article will show you how to build a geographical map using the Python Altair library.

The article will cover the following:

  • Downloading the map
  • Choropleth map
  • Dot density map
  • Proportional symbol map

To create a geographical map in Python Altair, you need a topoJSON file specifying the geographic boundaries of the regions on the map and a dataset with values for each region.

Creating the topoJSON file is beyond the scope of this article, but there are many resources already available on the web that provide ready-to-use maps, such as:

Once you have your topoJSON file, you can load it into Altair using the topo_feature() function:

import altair as alt

url = # url to a topoJSON file
feature = # feature name to extract

data_map = alt.topo_feature(url, feature)

The topo_feature() function receives as input the URL to the topoJSON file and the feature to extract. The feature is the string value corresponding to a child of the objects attribute in the topoJSON object. To retrieve the feature string , open the topoJSON object to search for the objects attribute.

A choropleth map is a geographical map where regions are colored according to a metric. For example, a choropleth map of the United States could show which states have the highest population density.

Consider the Life expectancy at birth dataset released by OECD Data as an open data dataset [1]. Use the following code to draw a choropleth map in Altair showing the life expectancy in Europe in 2010:

import pandas as pd
import altair as alt

alt.data_transformers.disable_max_rows()

df = pd.read_csv('data/life_expectancy_at_birth.csv')
df = df[df['Region'] == 'Europe & Central Asia']
df = df[df['Year'] == 2010]

url = "https://raw.githubusercontent.com/deldersveld/topojson/master/continents/europe.json"

data_map = alt.topo_feature(url, "continent_Europe_subunits")

alt.Chart(data_map).mark_geoshape().encode(
tooltip='properties.geounit:N',
color=alt.Color('Life Expectancy:Q')
).project('mercator').properties(
width=500,
height=300
).transform_lookup(
lookup='properties.geounit',
from_=alt.LookupData(df, 'Country', ['Country', 'Life Expectancy'])
)

Use the transform_lookup() function to merge the data_map dataset and the life expectancy dataset. The previous code builds the following map:

Image by author

A dot density map is a geographical map where each entity is represented by a dot. For example, a dot density map could show all the national parks in Europe. A dot would represent each park.

Consider the airports dataset, released as open data by OurAirports.com. Read more about the dataset license here. Use the following code to draw a dot density map in Altair:

import pandas as pd
import altair as alt
alt.data_transformers.disable_max_rows()

df = pd.read_csv('data/airports.csv')
df = df[df.type == 'small_airport']

url = "https://raw.githubusercontent.com/deldersveld/topojson/master/world-continents.json"
source = alt.topo_feature(url, "continent")

base = alt.Chart(source).mark_geoshape(
fill='lightgray',
stroke='white'
).project('mercator').properties(
width=800,
height=600
)

points = alt.Chart(df).mark_circle().encode(
longitude='longitude_deg:Q',
latitude='latitude_deg:Q',
size=alt.value(10),
tooltip='name:N',
color=alt.value('red')
)

(base + points).properties(title='Small airports in the world').configure_title(fontSize=20)

As a result, you’ll obtain the following map:

Image by author

A proportional symbol map is a dot density map where the size of the dot depends on a certain variable. For example, you can represent the number of airports for each country in the world as a variable-size dot. The bigger the dot representing a country, the greater the number of airports in that country.

Consider again the airports dataset, released as open data by OurAirports.com. Use the following code to draw a proportional symbol map in Altair:

import pandas as pd
import altair as alt
alt.data_transformers.disable_max_rows()

url = "https://raw.githubusercontent.com/deldersveld/topojson/master/world-continents.json"
source = alt.topo_feature(url, "continent")

base = alt.Chart(source).mark_geoshape(
fill='lightgray',
stroke='white'
).project('mercator').properties(
width=800,
height=600
)

df = pd.read_csv('data/airports.csv')
df = df[df.type == 'large_airport']
df2 = df.groupby(by=['iso_country'])['name'].count().to_frame().reset_index()
df2.rename(columns={'name' : 'number_of_airports'},inplace=True)

points = alt.Chart(df).mark_circle().encode(
longitude='longitude_deg:Q',
latitude='latitude_deg:Q',
size=alt.Size('number_of_airports:Q', title='# airports'),
color=alt.value('red')
).properties(
width=500,
height=400
).transform_lookup(
lookup='iso_country',
from_=alt.LookupData(df2, 'iso_country', ['iso_country', 'number_of_airports'])
)

base + points

Please notice that we use group by to group countries to calculate the number of airports for each country.

The following figure shows the resulting chart:

Image by author

Congratulations! You have just learned how to build a geographical map in Altair, using three different ways: a choropleth map, a dot density map, and a proportional symbol map.

Altair is a very useful library for data visualization. For example, you can use Altair to draw a time series. Read this article to learn 3 ways that you may not know to visualize a time series in Altair.

Also, you can use Altair to declutter a chart and transform it into a more readable chart. Read this article to learn how to declutter a bar chart in Altair.

If you have come this far to read, it is already a lot for today. Thanks! You can read more about me in this article.

If you like data visualization as I do, consider exploring data storytelling. Data storytelling is the art of telling stories through data. If you know enough about data visualization but want to move to data storytelling, read the book #Makeover Monday by A. Kriebel and Eva Murray. Read a personal review of the book here.

[1] OECD (2023), Life expectancy at birth (indicator). doi: 10.1787/27e0fc9d-en (Accessed on 18 January 2023)


Data Visualization, Geographical Data

A data visualization tutorial on how to build three different maps in Python Altair: choropleth map, dot density map, and proportional symbol map

Photo by Kyle Glenn on Unsplash

For data scientists, visualizing data is an essential skill. It helps to quickly understand patterns and correlations in the data that would otherwise be missed. Geographical maps are a great way to visualize spatial data and can be used to explore trends in different countries or regions.

This article will show you how to build a geographical map using the Python Altair library.

The article will cover the following:

  • Downloading the map
  • Choropleth map
  • Dot density map
  • Proportional symbol map

To create a geographical map in Python Altair, you need a topoJSON file specifying the geographic boundaries of the regions on the map and a dataset with values for each region.

Creating the topoJSON file is beyond the scope of this article, but there are many resources already available on the web that provide ready-to-use maps, such as:

Once you have your topoJSON file, you can load it into Altair using the topo_feature() function:

import altair as alt

url = # url to a topoJSON file
feature = # feature name to extract

data_map = alt.topo_feature(url, feature)

The topo_feature() function receives as input the URL to the topoJSON file and the feature to extract. The feature is the string value corresponding to a child of the objects attribute in the topoJSON object. To retrieve the feature string , open the topoJSON object to search for the objects attribute.

A choropleth map is a geographical map where regions are colored according to a metric. For example, a choropleth map of the United States could show which states have the highest population density.

Consider the Life expectancy at birth dataset released by OECD Data as an open data dataset [1]. Use the following code to draw a choropleth map in Altair showing the life expectancy in Europe in 2010:

import pandas as pd
import altair as alt

alt.data_transformers.disable_max_rows()

df = pd.read_csv('data/life_expectancy_at_birth.csv')
df = df[df['Region'] == 'Europe & Central Asia']
df = df[df['Year'] == 2010]

url = "https://raw.githubusercontent.com/deldersveld/topojson/master/continents/europe.json"

data_map = alt.topo_feature(url, "continent_Europe_subunits")

alt.Chart(data_map).mark_geoshape().encode(
tooltip='properties.geounit:N',
color=alt.Color('Life Expectancy:Q')
).project('mercator').properties(
width=500,
height=300
).transform_lookup(
lookup='properties.geounit',
from_=alt.LookupData(df, 'Country', ['Country', 'Life Expectancy'])
)

Use the transform_lookup() function to merge the data_map dataset and the life expectancy dataset. The previous code builds the following map:

Image by author

A dot density map is a geographical map where each entity is represented by a dot. For example, a dot density map could show all the national parks in Europe. A dot would represent each park.

Consider the airports dataset, released as open data by OurAirports.com. Read more about the dataset license here. Use the following code to draw a dot density map in Altair:

import pandas as pd
import altair as alt
alt.data_transformers.disable_max_rows()

df = pd.read_csv('data/airports.csv')
df = df[df.type == 'small_airport']

url = "https://raw.githubusercontent.com/deldersveld/topojson/master/world-continents.json"
source = alt.topo_feature(url, "continent")

base = alt.Chart(source).mark_geoshape(
fill='lightgray',
stroke='white'
).project('mercator').properties(
width=800,
height=600
)

points = alt.Chart(df).mark_circle().encode(
longitude='longitude_deg:Q',
latitude='latitude_deg:Q',
size=alt.value(10),
tooltip='name:N',
color=alt.value('red')
)

(base + points).properties(title='Small airports in the world').configure_title(fontSize=20)

As a result, you’ll obtain the following map:

Image by author

A proportional symbol map is a dot density map where the size of the dot depends on a certain variable. For example, you can represent the number of airports for each country in the world as a variable-size dot. The bigger the dot representing a country, the greater the number of airports in that country.

Consider again the airports dataset, released as open data by OurAirports.com. Use the following code to draw a proportional symbol map in Altair:

import pandas as pd
import altair as alt
alt.data_transformers.disable_max_rows()

url = "https://raw.githubusercontent.com/deldersveld/topojson/master/world-continents.json"
source = alt.topo_feature(url, "continent")

base = alt.Chart(source).mark_geoshape(
fill='lightgray',
stroke='white'
).project('mercator').properties(
width=800,
height=600
)

df = pd.read_csv('data/airports.csv')
df = df[df.type == 'large_airport']
df2 = df.groupby(by=['iso_country'])['name'].count().to_frame().reset_index()
df2.rename(columns={'name' : 'number_of_airports'},inplace=True)

points = alt.Chart(df).mark_circle().encode(
longitude='longitude_deg:Q',
latitude='latitude_deg:Q',
size=alt.Size('number_of_airports:Q', title='# airports'),
color=alt.value('red')
).properties(
width=500,
height=400
).transform_lookup(
lookup='iso_country',
from_=alt.LookupData(df2, 'iso_country', ['iso_country', 'number_of_airports'])
)

base + points

Please notice that we use group by to group countries to calculate the number of airports for each country.

The following figure shows the resulting chart:

Image by author

Congratulations! You have just learned how to build a geographical map in Altair, using three different ways: a choropleth map, a dot density map, and a proportional symbol map.

Altair is a very useful library for data visualization. For example, you can use Altair to draw a time series. Read this article to learn 3 ways that you may not know to visualize a time series in Altair.

Also, you can use Altair to declutter a chart and transform it into a more readable chart. Read this article to learn how to declutter a bar chart in Altair.

If you have come this far to read, it is already a lot for today. Thanks! You can read more about me in this article.

If you like data visualization as I do, consider exploring data storytelling. Data storytelling is the art of telling stories through data. If you know enough about data visualization but want to move to data storytelling, read the book #Makeover Monday by A. Kriebel and Eva Murray. Read a personal review of the book here.

[1] OECD (2023), Life expectancy at birth (indicator). doi: 10.1787/27e0fc9d-en (Accessed on 18 January 2023)

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.

Leave a comment