Recommended way to color unavailable data in choropleth_mapbox?

Hey all,

I managed to plot a px.choropleth_mapbox, but I’d like to color my missing values (say, with gray). What’s the best approach to this? Couldn’t find anything. My code looks like this:

with open("geojson_map.geojson") as geofile:
    geojson = json.load(geofile)

data_district=  pd.read_csv('data_district.csv')

fig = px.choropleth_mapbox(data_district, geojson=geojson, locations='district', color='cap_rate',
                           range_color=(0, data_district.cap_rate.max()),
                           center=dict(lon=-70.67159, lat=-33.47636),

                           # labels={'unemp':'unemployment rate'}
fig.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0})

This creates a nice map but with two missing districts:

In data_district those rows show like this:

  district            cap_rate
Lo Espejo         np.nan
Colina             np.nan

And they are included in the geojson data, too. How would I go to color those districts?

Thanks !

Hi @Juanouo I think you can use the method described in this post: transform the nans to a numerical value outside of your value range and define a custom colorscale with a specific color (for example gray) for the values which used to be nan. Please see also the tutorial on colorscales to see how to define a custom colorscale.

Thank you very much @Emmanuelle, that was exactly what I was looking for.

hey there,
I am facing currently the same problem, but I can’t get it to work. How did you solve this. @Emmanuelle your post seems nice but the px.choropleth_mapbox method doesn’t take a colorscale argument.

Hope you can help me.

Simple Solution the colorscale argument can just be added to the color_continuous_scale option.