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:
In [1]:
import folium
import pandas as pd
Country coordinates for plotting
source: https://github.com/python-visualization/folium/blob/master/examples/data/world-countries.json
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
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]:
In [4]:
data.head()
Out[4]:
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]:
In [6]:
plot_data = stage[['CountryCode','Value']]
plot_data.head()
Out[6]:
In [7]:
plot_data.describe()
Out[7]:
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']
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]:
More Folium Examples can be found at:
https://folium.readthedocs.io/en/latest/quickstart.html#getting-started
Documentation at:
https://media.readthedocs.org/pdf/folium/latest/folium.pdf
https://folium.readthedocs.io/en/latest/quickstart.html#getting-started
Documentation at:
https://media.readthedocs.org/pdf/folium/latest/folium.pdf
In [ ]:
For more details on folium map, see this link:
http://python-visualization.github.io/folium/docs-v0.5.0/modules.html#id2