✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
πŸ‡ Announcing Dash VTK for 3d simulation graphics. Check out the March webinar.

Px.choropleth() bug - map has holes

from urllib.request import urlopen
import json
import requests
import os
import pandas as pd
import plotly.express as px

with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response:
    counties = json.load(response)

tsfile = 'time_series_covid19_confirmed_US.csv'
tsurl = 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/' + tsfile

if not os.path.exists(tsfile):
    req = requests.get(tsurl)
    with open(tsfile, 'wb') as f:
ts = pd.read_csv(tsfile)

ts = ts[ts['FIPS'] < 80000].copy(deep=True)

ts_short = ts[['FIPS', '5/9/20', '5/10/20']].copy(deep=True)
ts_short['delta'] = ts_short['5/10/20'] - ts_short['5/9/20']
ts_short = ts_short[ts_short['delta'] >= 0].copy(deep=True)
dmin = ts_short['5/10/20'].min()
dmax = ts_short['5/10/20'].max()

fig = px.choropleth(ts_short, geojson=counties, locations='FIPS', color='5/10/20',
                           range_color=(dmin, dmax),



The map is rendered with big holes in it:

But with the same dataset, create_choropleth() works fine:

import plotly.figure_factory as pff
fig2 = pff.create_choropleth(fips=ts_short['FIPS'], values=ts_short['5/10/20'])

I would like to use px.choropleth() because it seems easier to control in terms of colors, but it fails to render the map properly.

Plotly 4.6.0
Python 3.7.7
Jupyter notebook

Nevermind, this was a combination of missing FIPS codes in the data, and a change from the old figure factory to the new plotly express choropleth(), where the new library doesn’t draw anything if data is missing, while the old create_choropleth() I think assumed missing data was 0 or minimum.

See complete explanation here: https://github.com/plotly/datasets/issues/29

Hi @FlorinAndrei, the FIPS code is supposed to be a 5-digit string, including with leading zeros (for example β€˜06021’ etc.). Here the FIPS columns is of type float hence the counties which should have a leading zero are not correctly represented (they get passed as β€˜6021’ which is not a valid FIPS code). In the code below I corrected the formatting and the result is fine (I also used the percentile 98 to have a better contrast)

ts_short['FIPS_str'] = ts_short['FIPS'].apply(lambda x: "%05d" %(int(x)))
from scipy import stats
score = stats.scoreatpercentile(ts_short['5/10/20'], 98)
fig = px.choropleth(ts_short, geojson=counties, locations='FIPS_str', color='5/10/20',
                           range_color=(dmin, score),


1 Like