plotly.figure_factory.create_choropleth

plotly.figure_factory.create_choropleth(fips, values, scope=['usa'], binning_endpoints=None, colorscale=None, order=None, simplify_county=0.02, simplify_state=0.02, asp=None, show_hover=True, show_state_data=True, state_outline=None, county_outline=None, centroid_marker=None, round_legend_values=False, exponent_format=False, legend_title='', **layout_options)

deprecated, use instead plotly.express.choropleth() with custom GeoJSON.

This function also requires shapely, geopandas and plotly-geo to be installed.

Returns figure for county choropleth. Uses data from package_data.

Parameters
  • fips ((list)) – list of FIPS values which correspond to the con catination of state and county ids. An example is ‘01001’.

  • values ((list)) – list of numbers/strings which correspond to the fips list. These are the values that will determine how the counties are colored.

  • scope ((list)) – list of states and/or states abbreviations. Fits all states in the camera tightly. Selecting [‘usa’] is the equivalent of appending all 50 states into your scope list. Selecting only ‘usa’ does not include ‘Alaska’, ‘Puerto Rico’, ‘American Samoa’, ‘Commonwealth of the Northern Mariana Islands’, ‘Guam’, ‘United States Virgin Islands’. These must be added manually to the list. Default = [‘usa’]

  • binning_endpoints ((list)) – ascending numbers which implicitly define real number intervals which are used as bins. The colorscale used must have the same number of colors as the number of bins and this will result in a categorical colormap.

  • colorscale ((list)) –

    a list of colors with length equal to the number of categories of colors. The length must match either all unique numbers in the ‘values’ list or if endpoints is being used, the number of categories created by the endpoints.

    For example, if binning_endpoints = [4, 6, 8], then there are 4 bins: [-inf, 4), [4, 6), [6, 8), [8, inf)

  • order ((list)) – a list of the unique categories (numbers/bins) in any desired order. This is helpful if you want to order string values to a chosen colorscale.

  • simplify_county ((float)) – determines the simplification factor for the counties. The larger the number, the fewer vertices and edges each polygon has. See http://toblerity.org/shapely/manual.html#object.simplify for more information. Default = 0.02

  • simplify_state ((float)) – simplifies the state outline polygon. See http://toblerity.org/shapely/manual.html#object.simplify for more information. Default = 0.02

  • asp ((float)) – the width-to-height aspect ratio for the camera. Default = 2.5

  • show_hover ((bool)) – show county hover and centroid info

  • show_state_data ((bool)) – reveals state boundary lines

  • state_outline ((dict)) – dict of attributes of the state outline including width and color. See https://plot.ly/python/reference/#scatter-marker-line for all valid params

  • county_outline ((dict)) – dict of attributes of the county outline including width and color. See https://plot.ly/python/reference/#scatter-marker-line for all valid params

  • centroid_marker ((dict)) – dict of attributes of the centroid marker. The centroid markers are invisible by default and appear visible on selection. See https://plot.ly/python/reference/#scatter-marker for all valid params

  • round_legend_values ((bool)) – automatically round the numbers that appear in the legend to the nearest integer. Default = False

  • exponent_format ((bool)) – if set to True, puts numbers in the K, M, B number format. For example 4000.0 becomes 4.0K Default = False

  • legend_title ((str)) – title that appears above the legend

  • **layout_options

    a **kwargs argument for all layout parameters

Example 1: Florida:

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/minoritymajority.csv'
)
df_sample_r = df_sample[df_sample['STNAME'] == 'Florida']

values = df_sample_r['TOT_POP'].tolist()
fips = df_sample_r['FIPS'].tolist()

binning_endpoints = list(np.mgrid[min(values):max(values):4j])
colorscale = ["#030512","#1d1d3b","#323268","#3d4b94","#3e6ab0",
            "#4989bc","#60a7c7","#85c5d3","#b7e0e4","#eafcfd"]
fig = ff.create_choropleth(
    fips=fips, values=values, scope=['Florida'], show_state_data=True,
    colorscale=colorscale, binning_endpoints=binning_endpoints,
    round_legend_values=True, plot_bgcolor='rgb(229,229,229)',
    paper_bgcolor='rgb(229,229,229)', legend_title='Florida Population',
    county_outline={'color': 'rgb(255,255,255)', 'width': 0.5},
    exponent_format=True,
)

Example 2: New England:

import plotly.figure_factory as ff

import pandas as pd

NE_states = ['Connecticut', 'Maine', 'Massachusetts',
            'New Hampshire', 'Rhode Island']
df_sample = pd.read_csv(
    'https://raw.githubusercontent.com/plotly/datasets/master/minoritymajority.csv'
)
df_sample_r = df_sample[df_sample['STNAME'].isin(NE_states)]
colorscale = ['rgb(68.0, 1.0, 84.0)',
'rgb(66.0, 64.0, 134.0)',
'rgb(38.0, 130.0, 142.0)',
'rgb(63.0, 188.0, 115.0)',
'rgb(216.0, 226.0, 25.0)']

values = df_sample_r['TOT_POP'].tolist()
fips = df_sample_r['FIPS'].tolist()
fig = ff.create_choropleth(
    fips=fips, values=values, scope=NE_states, show_state_data=True
)
fig.show()

Example 3: California and Surrounding States:

import plotly.figure_factory as ff

import pandas as pd

df_sample = pd.read_csv(
    'https://raw.githubusercontent.com/plotly/datasets/master/minoritymajority.csv'
)
df_sample_r = df_sample[df_sample['STNAME'] == 'California']

values = df_sample_r['TOT_POP'].tolist()
fips = df_sample_r['FIPS'].tolist()

colorscale = [
    'rgb(193, 193, 193)',
    'rgb(239,239,239)',
    'rgb(195, 196, 222)',
    'rgb(144,148,194)',
    'rgb(101,104,168)',
    'rgb(65, 53, 132)'
]

fig = ff.create_choropleth(
    fips=fips, values=values, colorscale=colorscale,
    scope=['CA', 'AZ', 'Nevada', 'Oregon', ' Idaho'],
    binning_endpoints=[14348, 63983, 134827, 426762, 2081313],
    county_outline={'color': 'rgb(255,255,255)', 'width': 0.5},
    legend_title='California Counties',
    title='California and Nearby States'
)
fig.show()

Example 4: USA:

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']
)

binning_endpoints = list(np.linspace(1, 12, len(colorscale) - 1))
colorscale = ["#f7fbff", "#ebf3fb", "#deebf7", "#d2e3f3", "#c6dbef",
            "#b3d2e9", "#9ecae1", "#85bcdb", "#6baed6", "#57a0ce",
            "#4292c6", "#3082be", "#2171b5", "#1361a9", "#08519c",
            "#0b4083","#08306b"]
fips = df_sample['FIPS']
values = df_sample['Unemployment Rate (%)']
fig = ff.create_choropleth(
    fips=fips, values=values, scope=['usa'],
    binning_endpoints=binning_endpoints, colorscale=colorscale,
    show_hover=True, centroid_marker={'opacity': 0},
    asp=2.9, title='USA by Unemployment %',
    legend_title='Unemployment %'
)
fig.show()