Oct 14, 2018

Example 15: Exploring Folium Map Plot

Using Folium Library for Geographic Overlays


Folium is a powerful Python library that helps to create several types of interactive maps in Python using Leaflet map library. This library is very useful in Data Science projects where we want to show country-wise statistical data on an interactive map.

Folium makes it easy to visualize data, that has been manipulated in Python, on an interactive Leaflet map as overlay, called choropleth visualization. It enables both the binding of data to a map for choropleth visualizations as well as passing rich vector/raster/html visualizations as markers on the map.

The library has a number of built-in tilesets from OpenStreetMap, Mapbox, and Stamen, and supports custom tilesets with Mapbox or Cloudmade API keys. Folium supports both Image, Video, GeoJSON and TopoJSON overlays. Thus, Folium builds on the data wrangling strengths of the Python ecosystem and the mapping strengths of the leaflet.js library.

So, manipulate your data in Python, and then visualize it in on a Leaflet map via folium, as this example shows for CO2 emissions per capita for worldwide countries:

Plotting CO2 Emissions per capita in the World Development Indicators dataset

In [1]:
import folium
import pandas as pd
In [2]:
# Path to the World Countries json Database
country_geo = './MLData/04_World_Map_Data/world-countries.json'
Data on world-development-indicators downloaded from Kaggle:
https://www.kaggle.com/worldbank/world-development-indicators
In [3]:
# Read in the World Development Indicators Database
data = pd.read_csv('./MLData/03_World-Development-Indicators/Indicators.csv')
data.shape
Out[3]:
(5656458, 6)
In [4]:
data.head()
Out[4]:
CountryName CountryCode IndicatorName IndicatorCode Year Value
0 Arab World ARB Adolescent fertility rate (births per 1,000 wo... SP.ADO.TFRT 1960 1.335609e+02
1 Arab World ARB Age dependency ratio (% of working-age populat... SP.POP.DPND 1960 8.779760e+01
2 Arab World ARB Age dependency ratio, old (% of working-age po... SP.POP.DPND.OL 1960 6.634579e+00
3 Arab World ARB Age dependency ratio, young (% of working-age ... SP.POP.DPND.YG 1960 8.102333e+01
4 Arab World ARB Arms exports (SIPRI trend indicator values) MS.MIL.XPRT.KD 1960 3.000000e+06
Pull out CO2 emisions for every country in 2011
In [5]:
# Select CO2 emissions for all countries in 2011
hist_indicator = 'CO2 emissions \(metric'
hist_year = 2011

mask1 = data['IndicatorName'].str.contains(hist_indicator) 
mask2 = data['Year'].isin([hist_year])

# Apply the masks
stage = data[mask1 & mask2]
stage.head()
Out[5]:
CountryName CountryCode IndicatorName IndicatorCode Year Value
5026275 Arab World ARB CO2 emissions (metric tons per capita) EN.ATM.CO2E.PC 2011 4.724500
5026788 Caribbean small states CSS CO2 emissions (metric tons per capita) EN.ATM.CO2E.PC 2011 9.692960
5027295 Central Europe and the Baltics CEB CO2 emissions (metric tons per capita) EN.ATM.CO2E.PC 2011 6.911131
5027870 East Asia & Pacific (all income levels) EAS CO2 emissions (metric tons per capita) EN.ATM.CO2E.PC 2011 5.859548
5028456 East Asia & Pacific (developing only) EAP CO2 emissions (metric tons per capita) EN.ATM.CO2E.PC 2011 5.302499

Setup our data for plotting

Create a data frame with just the country codes and the values we want to plot.
In [6]:
plot_data = stage[['CountryCode','Value']]
plot_data.head()
Out[6]:
CountryCode Value
5026275 ARB 4.724500
5026788 CSS 9.692960
5027295 CEB 6.911131
5027870 EAS 5.859548
5028456 EAP 5.302499
In [7]:
plot_data.describe()
Out[7]:
Value
count 232.000000
mean 4.883364
std 6.052161
min 0.021350
25% 0.829105
50% 2.960256
75% 6.711322
max 44.018926
Hence, the minimum indicator value is 0.021350 and the maximum value is 44.018926.
In [8]:
# Label for the legend
hist_indicator = stage.iloc[0]['IndicatorName']

Visualize CO2 emissions per capita using Folium

Folium provides interactive maps with the ability to create sophisticated overlays for data visualization
In [9]:
# Setup a folium map at a high-level zoom @Alok - what is the 100,0, doesn't seem like lat long

map = folium.Map(location=[100, 0], zoom_start=1.5)
In [10]:
# Choropleth maps bind Pandas Data Frames and json geometries.
# This allows us to quickly visualize data combinations.

map = folium.Map([20, 10], zoom_start=2)

map.choropleth(geo_data=country_geo, data=plot_data,
             columns=['CountryCode', 'Value'],
             key_on='feature.id',
             fill_color='YlOrRd', fill_opacity=0.7, line_opacity=0.2,
             legend_name=hist_indicator)
Note: Try use different ColorBrewer code as: YlGnBu, PuBuGn, YlOrRd, BuPu, OrRd, RdPu, Oranges, Purples, etc.
In [11]:
# Create Folium plot
map.save('plot_data.html')
In [12]:
# Import the Folium interactive html file
from IPython.display import HTML
HTML('<iframe src=plot_data.html width=730 height=450></iframe>')
Out[12]:
In [ ]:
 


For more details on folium map, see this link:
http://python-visualization.github.io/folium/docs-v0.5.0/modules.html#id2