Order of elements in plot

Dear community

I am rather new to plotly.
When plotting a scatter graph and then plotting a line, how do I set the order of the two elements? Currently, the points are on top but I want it the other way around. I did not find any hint on how to set the order in the forum.

Thank you so much
Chris

Hi @cvahlen,

Within scatter traces, the order of display on the screen should match the order in the figure’s data tuple. If you want the line on top, make sure the scatter trace displaying the line shows up later in the figure than the scatter trace showing the markers.

Something like,

import plotly.graph_objs as go
fig = go.Figure(
    data=[
        go.Scatter(mode='markers', ...),
        go.Scatter(mode='lines', ...),
    ]
)

Hope that helps,
-Jon

Hi @cvahlen

I use the following trick to reverse the order of the traces.

import plotly.graph_objs as go
fig = go.Figure(
    data=[
        go.Scatter(mode='markers', ...),
        go.Scatter(mode='lines', ...),
    ]
)
markers, line = fig.data
fig.data = line, markers

β€œfig.data” is a tuple and the code just creates a new tuple with the reverse order and assigns it back to β€œfig.data”.

A shorthand version of @conighion 's solution that works for any number of traces:
fig.data = fig.data[::-1]

1 Like