All examples By author By category About


Folium Choropleth

A choropleth map created with Folium and Pandas. Pull data into a dataframe, bind to a feature of the GeoJSON, map it. Folium allows you to specify any of the color brewer sequential color groups, and also allows you to specify the d3 quantize scale range:

import folium
import json
import pandas as pd
import vincent

county_data = r'us_county_data.csv'
county_geo = r'us-counties.json'

#We want to map the county codes we have in our geometry to those in the
#county_data file, which contains additional rows we don't need
with open(county_geo, 'r') as f:
    get_id = json.load(f)

county_codes = [x['id'] for x in get_id['features']]
county_df = pd.DataFrame({'FIPS_Code': county_codes}, dtype=str)

#Read into Dataframe, cast to string for consistency
df = pd.read_csv(county_data, na_values=[' '])
df['FIPS_Code'] = df['FIPS_Code'].astype(str)

#Perform an inner join, pad NA's with data from nearest county
merged = pd.merge(df, county_df, on='FIPS_Code', how='inner')
merged = merged.fillna(method='pad')

map = folium.Map(location=[39.8282, -98.5795], zoom_start=4)
map.geo_json(county_geo, data=merged,
             columns=['FIPS_Code', 'Unemployed_2011'], key_on='',
             fill_color='YlGnBu', line_opacity=0.3,
             quantize_range=[0, 5000])