Choropleth#
Example#
import ipyleaflet
import json
import pandas as pd
import os
import requests
from ipywidgets import link, FloatSlider
from branca.colormap import linear
def load_data(url, filename, file_type):
r = requests.get(url)
with open(filename, 'w') as f:
f.write(r.content.decode("utf-8"))
with open(filename, 'r') as f:
return file_type(f)
geo_json_data = load_data(
'https://raw.githubusercontent.com/jupyter-widgets/ipyleaflet/master/examples/us-states.json',
'us-states.json',
json.load)
unemployment = load_data(
'https://raw.githubusercontent.com/jupyter-widgets/ipyleaflet/master/examples/US_Unemployment_Oct2012.csv',
'US_Unemployment_Oct2012.csv',
pd.read_csv)
unemployment = dict(zip(unemployment['State'].tolist(), unemployment['Unemployment'].tolist()))
layer = ipyleaflet.Choropleth(
geo_data=geo_json_data,
choro_data=unemployment,
colormap=linear.YlOrRd_04,
border_color='black',
style={'fillOpacity': 0.8, 'dashArray': '5, 5'})
m = ipyleaflet.Map(center = (43,-100), zoom = 4)
m.add(layer)
m
Usage#
The Choropleth takes geo_data and choro_data as arguments.
The geo_data is a GeoJSON dictionary, for instance :
{
"type": "FeatureCollection",
"features":[{
"type":"Feature",
"id":"AL",
"properties":{"name":"Alabama"},
"geometry":{
"type":"Polygon",
"coordinates": [[[-87.359296,35.00118]]] ...
}
}]
}
The choro_data is a dictionary that maps an key to a float value, in order to build the colormap :
{'AL': 7.1,
'AK': 6.8}
The Choropleth layer is then created specifying on which key the colormap is applied:
Choropleth(
geo_data=geo_data,
choro_data=choro_data,
key_on='id'
)
Other options for the colormap can be found by running linear in the notebook cell. Refer to the Branca docs for more information about colormaps:
linear
| viridis | |
| plasma | |
| inferno | |
| magma | |
| Pastel1_03 | |
| Pastel1_05 | |
| Pastel1_04 | |
| Pastel1_07 | |
| YlOrRd_04 | |
| Pastel1_09 | |
| Pastel1_08 | |
| Spectral_07 | |
| RdYlBu_05 | |
| PuBuGn_03 | |
| Set1_08 | |
| PuBuGn_05 | |
| PuBuGn_04 | |
| PuBuGn_07 | |
| PuBuGn_06 | |
| PuBuGn_09 | |
| PuBuGn_08 | |
| YlOrBr_04 | |
| YlOrBr_05 | |
| Set1_07 | |
| YlOrBr_03 | |
| Set1_05 | |
| YlOrRd_03 | |
| PuOr_06 | |
| PuOr_07 | |
| PuOr_04 | |
| PuOr_05 | |
| PuOr_03 | |
| Purples_09 | |
| Set2_06 | |
| RdYlBu_11 | |
| PuOr_08 | |
| PuOr_09 | |
| Paired_03 | |
| RdBu_03 | |
| RdYlBu_10 | |
| Paired_07 | |
| Paired_06 | |
| Paired_05 | |
| Paired_04 | |
| Paired_09 | |
| Paired_08 | |
| RdGy_03 | |
| PiYG_04 | |
| Accent_03 | |
| BuGn_08 | |
| BuGn_09 | |
| BuGn_04 | |
| BuGn_05 | |
| BuGn_06 | |
| BuGn_07 | |
| BuGn_03 | |
| YlGnBu_07 | |
| YlGnBu_06 | |
| YlGnBu_05 | |
| YlGnBu_04 | |
| YlGnBu_03 | |
| RdBu_06 | |
| RdBu_05 | |
| RdBu_04 | |
| Accent_08 | |
| RdBu_09 | |
| RdBu_08 | |
| Set2_04 | |
| YlGnBu_09 | |
| YlGnBu_08 | |
| Blues_08 | |
| Blues_09 | |
| RdPu_09 | |
| RdPu_08 | |
| Set3_07 | |
| Set3_06 | |
| RdPu_05 | |
| RdPu_04 | |
| RdPu_07 | |
| RdPu_06 | |
| Blues_06 | |
| Blues_07 | |
| RdPu_03 | |
| Blues_05 | |
| Paired_10 | |
| Paired_11 | |
| Paired_12 | |
| PuBu_06 | |
| PuBu_07 | |
| PuBu_04 | |
| PuBu_05 | |
| PuRd_05 | |
| PuBu_03 | |
| PuRd_07 | |
| PuRd_06 | |
| PuRd_09 | |
| PuRd_08 | |
| Set2_07 | |
| PuBu_08 | |
| PuBu_09 | |
| RdBu_10 | |
| RdBu_11 | |
| Accent_06 | |
| Set3_03 | |
| Set3_05 | |
| Set3_12 | |
| Set3_10 | |
| Set3_04 | |
| RdGy_11 | |
| RdGy_10 | |
| Set1_03 | |
| Set1_09 | |
| Set3_09 | |
| BuPu_08 | |
| BuPu_09 | |
| RdYlGn_11 | |
| Blues_03 | |
| Set2_05 | |
| BuPu_03 | |
| BuPu_06 | |
| BuPu_07 | |
| BuPu_04 | |
| BuPu_05 | |
| Accent_04 | |
| YlOrRd_05 | |
| YlOrBr_08 | |
| Oranges_08 | |
| Oranges_09 | |
| Oranges_06 | |
| Oranges_07 | |
| Oranges_04 | |
| YlOrBr_09 | |
| Oranges_03 | |
| YlOrBr_06 | |
| Dark2_06 | |
| Blues_04 | |
| YlOrBr_07 | |
| RdYlGn_05 |