✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
⚡️ Concerned about the grid? Kyle Baranko teaches how to predicting peak loads using XGBoost. Register for the August webinar!

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',
                           featureidkey="properties.NOM_COM",
                           color_continuous_scale="Viridis",
                           range_color=(0, data_district.cap_rate.max()),
                           center=dict(lon=-70.67159, lat=-33.47636),


                           mapbox_style="carto-positron",
                           zoom=11,
                           opacity=0.5,
                           # 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.