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
viridis0.00.20.30.50.70.81.0
plasma0.00.20.30.50.70.81.0
inferno0.00.20.30.50.70.81.0
magma0.00.20.30.50.70.81.0
Pastel1_030.00.20.30.50.70.81.0
Pastel1_050.00.20.30.50.70.81.0
Pastel1_040.00.20.30.50.70.81.0
Pastel1_070.00.20.30.50.70.81.0
YlOrRd_040.00.20.30.50.70.81.0
Pastel1_090.00.20.30.50.70.81.0
Pastel1_080.00.20.30.50.70.81.0
Spectral_070.00.20.30.50.70.81.0
RdYlBu_050.00.20.30.50.70.81.0
PuBuGn_030.00.20.30.50.70.81.0
Set1_080.00.20.30.50.70.81.0
PuBuGn_050.00.20.30.50.70.81.0
PuBuGn_040.00.20.30.50.70.81.0
PuBuGn_070.00.20.30.50.70.81.0
PuBuGn_060.00.20.30.50.70.81.0
PuBuGn_090.00.20.30.50.70.81.0
PuBuGn_080.00.20.30.50.70.81.0
YlOrBr_040.00.20.30.50.70.81.0
YlOrBr_050.00.20.30.50.70.81.0
Set1_070.00.20.30.50.70.81.0
YlOrBr_030.00.20.30.50.70.81.0
Set1_050.00.20.30.50.70.81.0
YlOrRd_030.00.20.30.50.70.81.0
PuOr_060.00.20.30.50.70.81.0
PuOr_070.00.20.30.50.70.81.0
PuOr_040.00.20.30.50.70.81.0
PuOr_050.00.20.30.50.70.81.0
PuOr_030.00.20.30.50.70.81.0
Purples_090.00.20.30.50.70.81.0
Set2_060.00.20.30.50.70.81.0
RdYlBu_110.00.20.30.50.70.81.0
PuOr_080.00.20.30.50.70.81.0
PuOr_090.00.20.30.50.70.81.0
Paired_030.00.20.30.50.70.81.0
RdBu_030.00.20.30.50.70.81.0
RdYlBu_100.00.20.30.50.70.81.0
Paired_070.00.20.30.50.70.81.0
Paired_060.00.20.30.50.70.81.0
Paired_050.00.20.30.50.70.81.0
Paired_040.00.20.30.50.70.81.0
Paired_090.00.20.30.50.70.81.0
Paired_080.00.20.30.50.70.81.0
RdGy_030.00.20.30.50.70.81.0
PiYG_040.00.20.30.50.70.81.0
Accent_030.00.20.30.50.70.81.0
BuGn_080.00.20.30.50.70.81.0
BuGn_090.00.20.30.50.70.81.0
BuGn_040.00.20.30.50.70.81.0
BuGn_050.00.20.30.50.70.81.0
BuGn_060.00.20.30.50.70.81.0
BuGn_070.00.20.30.50.70.81.0
BuGn_030.00.20.30.50.70.81.0
YlGnBu_070.00.20.30.50.70.81.0
YlGnBu_060.00.20.30.50.70.81.0
YlGnBu_050.00.20.30.50.70.81.0
YlGnBu_040.00.20.30.50.70.81.0
YlGnBu_030.00.20.30.50.70.81.0
RdBu_060.00.20.30.50.70.81.0
RdBu_050.00.20.30.50.70.81.0
RdBu_040.00.20.30.50.70.81.0
Accent_080.00.20.30.50.70.81.0
RdBu_090.00.20.30.50.70.81.0
RdBu_080.00.20.30.50.70.81.0
Set2_040.00.20.30.50.70.81.0
YlGnBu_090.00.20.30.50.70.81.0
YlGnBu_080.00.20.30.50.70.81.0
Blues_080.00.20.30.50.70.81.0
Blues_090.00.20.30.50.70.81.0
RdPu_090.00.20.30.50.70.81.0
RdPu_080.00.20.30.50.70.81.0
Set3_070.00.20.30.50.70.81.0
Set3_060.00.20.30.50.70.81.0
RdPu_050.00.20.30.50.70.81.0
RdPu_040.00.20.30.50.70.81.0
RdPu_070.00.20.30.50.70.81.0
RdPu_060.00.20.30.50.70.81.0
Blues_060.00.20.30.50.70.81.0
Blues_070.00.20.30.50.70.81.0
RdPu_030.00.20.30.50.70.81.0
Blues_050.00.20.30.50.70.81.0
Paired_100.00.20.30.50.70.81.0
Paired_110.00.20.30.50.70.81.0
Paired_120.00.20.30.50.70.81.0
PuBu_060.00.20.30.50.70.81.0
PuBu_070.00.20.30.50.70.81.0
PuBu_040.00.20.30.50.70.81.0
PuBu_050.00.20.30.50.70.81.0
PuRd_050.00.20.30.50.70.81.0
PuBu_030.00.20.30.50.70.81.0
PuRd_070.00.20.30.50.70.81.0
PuRd_060.00.20.30.50.70.81.0
PuRd_090.00.20.30.50.70.81.0
PuRd_080.00.20.30.50.70.81.0
Set2_070.00.20.30.50.70.81.0
PuBu_080.00.20.30.50.70.81.0
PuBu_090.00.20.30.50.70.81.0
RdBu_100.00.20.30.50.70.81.0
RdBu_110.00.20.30.50.70.81.0
Accent_060.00.20.30.50.70.81.0
Set3_030.00.20.30.50.70.81.0
Set3_050.00.20.30.50.70.81.0
Set3_120.00.20.30.50.70.81.0
Set3_100.00.20.30.50.70.81.0
Set3_040.00.20.30.50.70.81.0
RdGy_110.00.20.30.50.70.81.0
RdGy_100.00.20.30.50.70.81.0
Set1_030.00.20.30.50.70.81.0
Set1_090.00.20.30.50.70.81.0
Set3_090.00.20.30.50.70.81.0
BuPu_080.00.20.30.50.70.81.0
BuPu_090.00.20.30.50.70.81.0
RdYlGn_110.00.20.30.50.70.81.0
Blues_030.00.20.30.50.70.81.0
Set2_050.00.20.30.50.70.81.0
BuPu_030.00.20.30.50.70.81.0
BuPu_060.00.20.30.50.70.81.0
BuPu_070.00.20.30.50.70.81.0
BuPu_040.00.20.30.50.70.81.0
BuPu_050.00.20.30.50.70.81.0
Accent_040.00.20.30.50.70.81.0
YlOrRd_050.00.20.30.50.70.81.0
YlOrBr_080.00.20.30.50.70.81.0
Oranges_080.00.20.30.50.70.81.0
Oranges_090.00.20.30.50.70.81.0
Oranges_060.00.20.30.50.70.81.0
Oranges_070.00.20.30.50.70.81.0
Oranges_040.00.20.30.50.70.81.0
YlOrBr_090.00.20.30.50.70.81.0
Oranges_030.00.20.30.50.70.81.0
YlOrBr_060.00.20.30.50.70.81.0
Dark2_060.00.20.30.50.70.81.0
Blues_040.00.20.30.50.70.81.0
YlOrBr_070.00.20.30.50.70.81.0
RdYlGn_05