Folium
is apython
library for interactive geo-spatial data visualization. It is a wrapper of theleaflet.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
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
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')