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:
        f.write(req.content)
ts = pd.read_csv(tsfile)

ts.dropna(inplace=True)
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',
                           color_continuous_scale="Viridis",
                           range_color=(dmin, dmax),
                           scope="usa"
                          )

fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})

fig.show()

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'])
fig2.show()

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
Anaconda
Windows

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',
                           color_continuous_scale="Viridis",
                           range_color=(dmin, score),
                           scope="usa"
                          )

fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})

fig.show()
1 Like