Choropleth Map

Some preliminaries

If you are wondering why in the world this webpage looks the way it does, it might help you to review Anaconda, Jupyter scripts and a basic Python example. You can do so by reviewing the post(s) below.

You can download this script at the bottom of this post.

Using Plotly

In this post we implement something called a "Choropleth" chart. You can learn more about these charts here:

These charts are incredibly useful for indicating data through colors on a geographic map. A visual geographical representation can be far more effective than a table of data, as the example below shows.

The chart below is developed using Plotly (https://plot.ly). This is a "freemium" service (i.e. free for some functionality, with premium option that must be purchased). My understanding is that in the basic version the charts you create are public (but the data remains private). You will need to create a free account on their website in order to get a username and API Key and begin using plotly.

You will have to initialize plotly the first time you use it, you can learn more about how to do it here: https://plot.ly/python/getting-started/#initialization-for-online-plotting

You might have to install the geopandas, plotly, and pyshp packages in order to run the code below. I had to run the following commands, one after the other: Go to start > Anaconda Prompt > conda install plotly (hit enter), then: conda install geopandas, and finally: conda install pyshp

The example below is taken (entirely) from the Plotly website. This, and more examples, are available here:

In [15]:
#initializing plotly (see above)
#import plotly 
#plotly.tools.set_credentials_file(username='Your Account', api_key='*******')
In [5]:
# Copy/pasted from: https://plot.ly/python/choropleth-maps/
import plotly.plotly as py
import plotly.figure_factory as ff

import numpy as np
import pandas as pd

df_sample = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/laucnty16.csv')
df_sample['State FIPS Code'] = df_sample['State FIPS Code'].apply(lambda x: str(x).zfill(2))
df_sample['County FIPS Code'] = df_sample['County FIPS Code'].apply(lambda x: str(x).zfill(3))
df_sample['FIPS'] = df_sample['State FIPS Code'] + df_sample['County FIPS Code']

colorscale = ["#f7fbff","#ebf3fb","#deebf7","#d2e3f3","#c6dbef","#b3d2e9","#9ecae1",
              "#85bcdb","#6baed6","#57a0ce","#4292c6","#3082be","#2171b5","#1361a9",
              "#08519c","#0b4083","#08306b"]
endpts = list(np.linspace(1, 12, len(colorscale) - 1))
fips = df_sample['FIPS'].tolist()
values = df_sample['Unemployment Rate (%)'].tolist()

fig = ff.create_choropleth(
    fips=fips, values=values,
    binning_endpoints=endpts,
    colorscale=colorscale,
    show_state_data=False,
    show_hover=True, centroid_marker={'opacity': 0},
    asp=2.9, title='USA by Unemployment %',
    legend_title='% unemployed'
)
py.iplot(fig, filename='choropleth_full_usa')
The draw time for this plot will be slow for clients without much RAM.
Out[5]:
In [ ]: