How to set map_center and map_zoom when using add_traces?

I followed the answer here to combine different plots into one:

This seems to work great, but the problem I’m having is that when I use this method the figure always start zoomed all the way out (So I can see the entire world) and thus I have to center the map on the area I want and zoom all the way in, which takes a long time whenever I want to see my data.

I have tried to use both map_center and map_zoom in the fig.update_layout() and I have added overwrite=True, just in case they were somehow set previously, but none of it seems to make any difference.

I have also tried to add zoom levels and center to each figure, but that also does not seem to matter.
So now I’m all out of ideas and not really sure what is going on here.

Does anyone have any suggestions?

Current code (with just one plot in it, since it fails already for one):


    fig = go.Figure()
    map_scatter = px.scatter_mapbox(df_sensors,
                            lat='lat',
                            lon='lon',
                            color_discrete_sequence=['red'],
                            zoom=12)

    fig.add_traces(map_scatter.data)
    fig.update_layout(mapbox_style="open-street-map",
                      overwrite=True,
                      map_center={'lat': 56.176, 'lon': 9.554},
                      map_zoom=12)
    fig.show()

Hey @tueboesen mixing graph_objects and express is not always straight forward.

Could you add some code we might try on our end to reproduce your issue?

Here is a simple example of what I encounter:


import pandas as pd
import plotly.express as px
import plotly.graph_objects as go

df_sensors = pd.DataFrame({'ID': [1, 2, 3, 4, 5],'lat': [56.176, 56.176, 56.176, 56.176, 56.176], 'lon': [9.554, 9.554, 9.554, 9.554, 9.554]})


fig = go.Figure()
map_scatter = px.scatter_mapbox(df_sensors,
                        lat='lat',
                        lon='lon',
                        color_discrete_sequence=['red'],
                        zoom=12)

fig.add_traces(map_scatter.data)
fig.update_layout(mapbox_style="open-street-map",
                  overwrite=True,
                  map_center={'lat': 56.176, 'lon': 9.554},
                  map_zoom=12)
fig.show()

When I run the above I end up with the following:

Did you try using scatter_map instead of scatter_mapbox as the latter is deprecated?

I did not, because I did not know scatter_mapbox was deprecated and neither did I know that there was a function called scatter_map. (I don’t see any warnings about it being deprecated when I run my code? or any warnings inside the function definition about it).

Your suggestion to use scatter_map worked out of the box and also fixed the issue with zoom, so thank you very much for this :slight_smile:

1 Like

Frankly I donΒ΄t know why you did not see the deprecation warning. I tried your code in jupyter lab and saw the message.

Anyways, happy I could be of help!