Add_scattermapbox to px.choropleth_mapbox figure seems to break redisplay of figure with new map

I have a complex Dash plotly app that has a choropleth_mapbox figure of a US county and its towns colored by town specific numeric values. The user can select a town from a dropdown list and display it in a dcc.Graph component via a callback. The user can repeatedly select different towns in the dropdown and the town map is displayed correctly. Great.

Now I want to display points of interest on the choropleth map as well., so I"ve added code to the function that creates the map (using fig.add_scattermapbox(…). I have only 2 towns (out of 30) that have the POI data, so the code only adds the scattermapbox for towns that have the data. Here’s the code:

def create_town_map_figure_px(df_town_data, locations_field, town_json, town_latitude, town_longitude, town_zoom,
                              selected_town, df_town_markers):
    print('\nfunction create_town_map_figure_px for ' + selected_town)

    fig = px.choropleth_mapbox(df_town_data, geojson=town_json, locations=locations_field, color='Actual Pct',
                               center={"lat": town_latitude, "lon": town_longitude},
                               range_color=[0, 1],
                               hover_data={'County': True, 'Town': True, 'Actual Pct': ':.2%'},
    fig = fig.update_layout(margin={"r": 1, "t": 1, "l": 1, "b": 1})

    if len(df_town_markers) > 0:
            text=df_town_markers['Marker Description'],
            # marker_color='rgb(235, 0, 100)'

    return fig

On starting the app, if I select only towns without the POI data, the app continues to act properly (allowing switching between towns at will).

  • If I then select one of the towns with POI data, it is displayed correctly (choropleth map is colored appropriately and the POIs are also displayed.
  • Now if I select any other town, the new map isn’t displayed, and the previous map is redisplayed.

Interestingly, if I create the choropleth map using graph objects, then add the scattermapbox to it, I’m able to select any town and display it (meaning displaying a town with the POI data doesn’t prevent showing any other town. But showing a town with POI data causes the choropleth map to lose its color, for any towns displayed thereafter also lack color.

Any ideas on the cause and what I might do to fix it? Is it possible to reset plotly before displaying a new town?

I forgot to mention I’m using Python 3.11, Dash 2.11.1, Dash-core-components 2.11.1, plotly 5.15.0.