Multiple Mapbox Figures in one map

Hi, I have a scatter_mapbox:

import plotly.express as px

fig = px.scatter_mapbox(df_sample,
lat=“lat”,
lon=“lng”,
hover_name=“id”,
color=“company”,
hover_data=[“id”],
zoom=10.5,
height=500)
fig.update_layout(mapbox_style=“carto-darkmatter”)
fig.update_layout(margin={“r”:0,“t”:0,“l”:0,“b”:0})
fig.show()

and a line_mapbox:

fig2 = px.line_mapbox(df, lat=“lat”, lon=“lng”,zoom=8, height=600)

fig2.update_layout(mapbox_style=“carto-darkmatter”, mapbox_zoom=8,
margin={“r”:0,“t”:0,“l”:0,“b”:0})
fig2.show()

How can i fit both into one map? I cannot quite figure this one out.

Hi @Sipsip, welcome to the forum! px functions return a Figure object, composed of traces and layout. If you want to superimpose the scatter and the line plot you can par example do

import plotly.express as px

fig = px.scatter_mapbox(df_sample,
lat=“lat”,
lon=“lng”,
hover_name=“id”,
color=“company”,
hover_data=[“id”],
zoom=10.5,
height=500)
fig2 = px.line_mapbox(df, lat=“lat”, lon=“lng”,zoom=8)
fig.add_trace(fig2.data[0]) # adds the line trace to the first figure
fig.update_layout(mapbox_style=“carto-darkmatter”)
fig.update_layout(margin={“r”:0,“t”:0,“l”:0,“b”:0})
1 Like

Please also see https://plot.ly/python/creating-and-updating-figures/ for more details.

Thank you this really helped a lot!

1 Like