Skip to content
Search
Generic filters
Exact matches only

A Step by Step Guide to Interactive Choropleth Map in Python | by Rashida Nasrin Sucky | Aug, 2020

Learn to Develop Choropleth Map Easily Using Python’s Folium Library

Rashida Nasrin Sucky

What is a choropleth map?

Here is the definition from Wikipedia:

Data Preparation

Data preparation is an important and common task for all data scientists. The dataset I used here is reasonably nice and clean. But for this visualization, I still need to work on it a bit. Let’s import the necessary libraries and the dataset.

import pandas as pd
import numpy as np
df = pd.read_excel('https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/DV0101EN/labs/Data_Files/Canada.xlsx',
sheet_name='Canada by Citizenship',
skiprows=range(20),
skipfooter=2)
df.columns#Output:
Index(['Type', 'Coverage', 'OdName', 'AREA', 'AreaName', 'REG', 'RegName', 'DEV', 'DevName', 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013],
dtype='object')
df.drop(['AREA', 'REG', 'DEV', 'Type', 'Coverage', 'AreaName', 'RegName', 'DevName'], axis=1, inplace=True)
df.rename(columns={'OdName':'Country'}, inplace=True)
df['Total'] = df.sum(axis=1)

Basic Choropleth Map

I am going to show, how to develop a choropleth map step by step here. Import folium. If you do not have folium, install it by running this command in your anaconda prompt:

conda install -c conda-forge folium
import folium
world = folium.Map(location=[0,0], zoom_start=2)
wc = r'world-countries.json'
  1. dataset,
  2. columns we need to use from the dataset,
  3. ‘key_on’ parameter that comes from the geo_data. The value of the ‘key_on’ parameter always starts with ‘feature’. Then we need to add the key from the geo_data that we saved as ‘wc’. That JSON file is too big. So, I am showing a part of it to explain the key_on parameter:
{"type":"Feature","properties":{"name":"Afghanistan"},"geometry":{"type":"Polygon","coordinates":[[[61.210817,35.650072],[62.230651,35.270664],[62.984662,35.404041],[63.193538,35.857166],[63.982896,36.007957],[64.546479,36.312073],[64.746105,37.111818],[65.588948,37.305217],[65.745631,37.661164],[66.217385,37.39379],[66.518607,37.362784],[67.075782,37.356144],[67.83,37.144994],[68.135562,37.023115],[68.859446,37.344336],[69.196273,37.151144],[69.518785,37.608997],[70.116578,37.588223],[70.270574,37.735165],[70.376304,38.138396],[70.806821,38.486282],[71.348131,38.258905],[71.239404,37.953265],[71.541918,37.905774],[71.448693,37.065645],[71.844638,36.738171],[72.193041,36.948288],[72.63689,37.047558],[73.260056,37.495257],[73.948696,37.421566],[74.980002,37.41999],[75.158028,37.133031],[74.575893,37.020841],[74.067552,36.836176],[72.920025,36.720007],[71.846292,36.509942],[71.262348,36.074388],[71.498768,35.650563],[71.613076,35.153203],[71.115019,34.733126],[71.156773,34.348911],[70.881803,33.988856],[69.930543,34.02012],[70.323594,33.358533],[69.687147,33.105499],[69.262522,32.501944],[69.317764,31.901412],[68.926677,31.620189],[68.556932,31.71331],[67.792689,31.58293],[67.683394,31.303154],[66.938891,31.304911],[66.381458,30.738899],[66.346473,29.887943],[65.046862,29.472181],[64.350419,29.560031],[64.148002,29.340819],[63.550261,29.468331],[62.549857,29.318572],[60.874248,29.829239],[61.781222,30.73585],[61.699314,31.379506],[60.941945,31.548075],[60.863655,32.18292],[60.536078,32.981269],[60.9637,33.528832],[60.52843,33.676446],[60.803193,34.404102],[61.210817,35.650072]]]},"id":"AFG"}
world.choropleth(geo_data=wc,
data=df,
columns=['Country', 'Total'],
key_on='feature.properties.name',
fill_color='YlOrRd',
fill_opacity=0.8,
line_opacity=0.2,
legend_name='Immigration to Canada'
)
world

Add Tiles

This map may look a bit plane. We can use tiles to make it look more interesting:

world_map = folium.Map(location=[0, 0], zoom_start=2, tiles='stamenwatercolor')
world_map.choropleth(geo_data=wc,
data=df,
columns=['Country', 'Total'],
threshold_scale=threshold_scale,
key_on='feature.properties.name',
fill_color='YlOrRd',
fill_opacity=0.7,
line_opacity=0.2,
legend_name='Immigration to Canada'
)
world = folium.Map(location=[0, 0], zoom_start=2, tiles='cartodbpositron')
tiles = ['stamenwatercolor', 'cartodbpositron', 'openstreetmap', 'stamenterrain']
for tile in tiles:
folium.TileLayer(tile).add_to(world)

world.choropleth(
geo_data=wc,
data=df,
columns=['Country', 'Total'],
threshold_scale=threshold_scale,
key_on='feature.properties.name',
fill_color='YlOrRd',
fill_opacity=0.7,
line_opacity=0.2,
legend_name='Immigration to Canada',
smooth_factor=0
)

folium.LayerControl().add_to(world)
world

Add Informative Label

Finally, I want to show you another useful and interesting option. That is to use an informative label. We cannot expect everyone to know the name of the country by looking at the map. It will be useful to have the label of the country on the map. We will make it interesting. Folium has a function called ‘GeoJsonTooltip’ that does that. First, we need to make the world map as usual. Add all the parameters to it and save in a variable. Then add this additional feature using ‘GeoJsonTooltip’ with an add_child method. Here is the complete code.

world = folium.Map(location=[0,0], zoom_start=2, tiles='cartodbpositron')
choropleth = folium.Choropleth(geo_data=wc,
data=df,
columns=['Country', 'Total'],
threshold_scale=threshold_scale,
key_on='feature.properties.name',
fill_color='YlOrRd',
fill_opacity=0.7,
line_opacity=0.2,
legend_name='Immigration to Canada',
).add_to(world)
choropleth.geojson.add_child(
folium.features.GeoJsonTooltip(['name'], labels=False))
world

Conclusion

I wanted to show how to develop an interactive choropleth map, style it, and add informative labels to it. I hope it was helpful.

Reading Recommendation