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',
                               color_continuous_scale=max_50_pct_color_scale,
                               mapbox_style="carto-positron",
                               zoom=town_zoom,
                               center={"lat": town_latitude, "lon": town_longitude},
                               opacity=0.5,
                               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})
    fig.update_coloraxes(colorbar_tickformat='.0%')

    if len(df_town_markers) > 0:
        fig.add_scattermapbox(
            lat=df_town_markers['Latitude'],
            lon=df_town_markers['Longitude'],
            mode='markers+text',
            text=df_town_markers['Marker Description'],
            marker_size=25,
            # marker_color='rgb(235, 0, 100)'
            marker_color='green'
        )

    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.