How to draw a geographical map in Altair

Altair, a Python library dedicated to statistical visualization, offers a declarative approach enabling users to effortlessly craft a diverse array of interactive and static visual representations. Seamlessly integrating with pandas DataFrames, it supports many plot types and boasts comprehensive customization capabilities for generating graphics of publication caliber. In Python, Altair stands out as a declarative statistical visualization library, empowering users to craft extensive visualizations, including geographical maps.

Geographical map in Altair

A geographical map in Altair typically means creating a visualization that displays geographical features such as countries, states, cities, etc., often overlaid with data or other visual elements. These maps can be used to visualize various geospatial datasets or analyze geographic patterns.

Geographical map of the population from European countries
Geographical map of the population from European countries

In Altair, we can create geographical maps by loading geospatial data (usually in GeoJSON format) and then using Altair’s plotting capabilities to visualize this data. We can customize the map’s appearance, add layers, apply different projections, and encode additional data attributes to create informative and visually appealing maps.

Example

We can use the mark_geoshape() function to draw a geographical map in Altair. The following code generates a geographical map using Altair to visualize the dummy population data of European countries.

import pandas as pd
import altair as alt
import os
# Create a dummy DataFrame for the population data
population_data = {
'Country': ['Germany', 'France', 'Spain', 'Italy', 'United Kingdom'],
'Population': [83783942, 65273511, 46754778, 60461826, 67886011]
}
df_population = pd.DataFrame(population_data)
# Define the URL for the GeoJSON file containing European country boundaries
url = "https://www.educative.io/udata/kkl6V4jrDkn/europe.json"
# Build the chart
source = alt.topo_feature(url, 'continent_Europe_subunits')
chart = alt.Chart(source).mark_geoshape().encode(
tooltip='properties.geounit:N',
color=alt.Color('Population:Q', scale=alt.Scale(scheme='greenblue'))
).project('mercator').properties(
width=500,
height=300
).transform_lookup(
lookup='properties.geounit',
from_=alt.LookupData(df_population, 'Country', ['Population'])
).properties(title='Population of European Countries')
chart.save('chart.html')
os.system('cat chart.html')

Explanation

  • Lines 1–3: We import Altair and the necessary libraries.

  • Lines 5–10: We create a dummy DataFrame named df_population containing two columns: Country and Population. This DataFrame represents the dummy population data for Germany, France, Spain, Italy, and the United Kingdom.

  • Line 13: We set the URL to retrieve the GeoJSON file containing the boundaries of European countries. We can access this file here.

  • Line 16: We load the GeoJSON file as a source for geographical features.

  • Lines 18–27: We create an Altair chart with the population data. We create a geographical map of European countries colored by population density. Let’s break down each part:

    • alt.Chart(source).mark_geoshape(): This initializes the chart object and specifies that it's a geographical map (mark_geoshape()). The source likely refers to the GeoJSON or TopoJSON data containing the geometries of European countries.

    • .encode(): This method is used to encode data properties to visual properties. In this case:

      • tooltip='properties.geounit:N': Specifies that the tooltip should display the name of the geographical unit (geounit) from the properties of the GeoJSON/TopoJSON data.

      • color=alt.Color('Population:Q', scale=alt.Scale(scheme='greenblue')): Specifies the color encoding for the map. It maps the Population data field to colors using a quantitative scale (Q), and it uses the greenblue color scheme.

    • .project('mercator'): This specifies the map projection to be used. Mercator projection is commonly used for world maps as it preserves shapes reasonably well while sacrificing area accuracy, making it suitable for navigation purposes.

    • .properties(width=500, height=300): This sets the width and height of the visualization to be 500 pixels wide and 300 pixels high.

    • .transform_lookup(): This transformation is used to lookup additional data from a DataFrame (df_population). It matches the 'geounit' field from the GeoJSON/TopoJSON data with the 'Country' field from the DataFrame and adds the 'Population' data to the visualization.

    • .properties(title='Population of European Countries'): This sets the title of the visualization to 'Population of European Countries'.

  • Line 29: We save the chart using chart.save('chart.html'). It exports the chart to an HTML file named chart.html.

  • Line 30: We display the chart on the console.

Overall, this code demonstrates how to create a geographical map visualizing the population data of European countries using Altair and a GeoJSON file.

Unlock your potential: Data visualization with the Altair series, all in one place!

To continue your exploration of data visualization using the Altair library, check out our series of Answers below:

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved