Choropleth map with Python and Folium

logo of a chart:Choropleth

This blogpost explains how to build a choropleth map of the US with python. It uses the Folium library that allows to create interactive map.

Folium is a python library for interactive geo-spatial data visualization. It is a wrapper of the leaflet.js javascript library. You can read more about it in the map section of the gallery.

Map initialization

A map made with Folium always starts with an initialization step where the tile and the location are defined:

# import the folium library
import folium

# initialize the map and store it in a m object
m = folium.Map(location=[40, -95], zoom_start=4)

# show the map
m
Make this Notebook Trusted to load map: File -> Trust Notebook

Here we are, now, let's show states with some appropriate colors to get a chloropleth.

Data

To build a choropleth map, you need 2 data inputs:

  • a set of geographic regions and their boundary coordinates
  • a numeric value for each region, used for the color

This blogpost is based on the official documentation, and aims at visualizing the unemployment rate in the US states. Let's load the 2 dataset:

import pandas as pd

url = (
    "https://raw.githubusercontent.com/python-visualization/folium/main/examples/data"
)
state_geo = f"{url}/us-states.json"
state_unemployment = f"{url}/US_Unemployment_Oct2012.csv"
state_data = pd.read_csv(state_unemployment)

Choropleth of US states

folium.Choropleth(
    geo_data=state_geo,
    name="choropleth",
    data=state_data,
    columns=["State", "Unemployment"],
    key_on="feature.id",
    fill_color="YlGn",
    fill_opacity=0.7,
    line_opacity=.1,
    legend_name="Unemployment Rate (%)",
).add_to(m)

folium.LayerControl().add_to(m)

m

Make this Notebook Trusted to load map: File -> Trust Notebook

Note: the official doc has some more examples for choropleth map with folium

Save the choropleth

You can save the above choropleth map as a standalone html file if needed:

m.save('../../static/interactiveCharts/292-choropleth-map-with-folium.html')

Contact & Edit


👋 This document is a work by Yan Holtz. You can contribute on github, send me a feedback on twitter or subscribe to the newsletter to know when new examples are published! 🔥

This page is just a jupyter notebook, you can edit it here. Please help me making this website better 🙏!