Updating Discrete Color Scales choropeth_mapbox

Hi forum,

We are working on a dashboard in Django Plotly Dash with a Choropleth Mapbox map. Now we are trying to update the data (and geometries) loaded in based on a dropdown. It works, but when switching between the dropdown option the selecting on the map no longer works…

The problem seems to be that the datasets don’t all contain all possible categorical values on which the colors are based ([‘80% - 90%’ ‘0% - 70%’ ‘70% - 80%’ ‘90% - 100%’ ‘> 100%’]), i.e. df1 contains [ ‘90% - 100%’ ‘> 100%’] and df2 contains [‘80% - 90%’ ‘0% - 70%’].

Is there some way to fix this without adding dummy geometries so that all datasets contain the same categorical variables? Like always using all categorical values, instead of basing it on the data.

Thanks in advance!

    fig = px.choropleth_mapbox(
        gdf, 
        geojson=gdf.geometry, 
        locations=gdf.index,
        color='categorical_variable', 
        color_discrete_map=color_discrete_map, #Maps all possible categorical values to color
        category_orders=category_orders
        ....
    )