Plotly Express multiple plots overlay

If I want to have two plotly express figures plotted on the same axis. What should I do.

I tried this:

fig = px.scatter_mapbox(points, lat="ycoord", lon="xcoord")
fig.add_trace(px.scatter_mapbox(stations, lat="ycoord", lon="xcoord",))
fig

But it is not working. I have two points dataset and want to see both on the same map.

Hi @shakasom, welcome to the forum! plotly express functions return a figure object (with data and layout). So, to add a trace to your existing figure created by plotly express, you can either

fig.add_trace(px.scatter_mapbox(stations, lat="ycoord", lon="xcoord",).data[0])
2 Likes

Thanks @Emmanuelle. I understood and successfully used the second option. You add the trace with fig.data and by the way I stumbled upon that colors and markers can be changed using fig.data. That was cool.

1 Like

This is very helpful. However, if I want to change the colors and formatting of ONLY the added_trace, how can I do that? data[0] obviously appears to omit the layout.

For example, I want a red line graph of activity over a month, with blue bar graphs showing the weekend days. How can I do that?

Also, how can I add the second chart’s color(s) to the legend?

You can specify the colors in you px.scatter with

color_discrete_sequence=px.colors.qualitative.Dark2

see here for different color scheme names.

When I did this, it automatically added to the legend.

This was the only real post I could find online close to the issue I am having. I could not locate a functioning answer within the documentation either. How does one overlay two scattermapboxes? add_trace will only allow me to add one element [as shown in your post].

fig1 = px.scatter_mapbox (Random Coordinates Set 1)
fig2 = px.scatter_mapbox (Random Coordinates Set 2)

I just want to display these on the same map/figure/surface however is correct verbage for the end product.

Hi @Emmanuelle thanks your reply vey helpful