# Order of elements in plot

**URL:** <https://community.plotly.com/t/order-of-elements-in-plot/25011>\
**Category:** 📊 Plotly Python\
**Created:** [June 25, 2019, 9:34am UTC](https://community.plotly.com/t/order-of-elements-in-plot/25011 "2019-06-25T09:34:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cvahlen](https://avatars.discourse-cdn.com/v4/letter/c/4bbf92/32.png) [@cvahlen](https://community.plotly.com/u/cvahlen)\
**Post date:** [June 25, 2019, 9:34am UTC](https://community.plotly.com/t/order-of-elements-in-plot/25011/1 "2019-06-25T09:34:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jmmease](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/jmmease/32/3706_2.png) [@jmmease](https://community.plotly.com/u/jmmease)\
**Post date:** [June 28, 2019, 9:49am UTC](https://community.plotly.com/t/order-of-elements-in-plot/25011/2 "2019-06-28T09:49:40Z")

</div>

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,

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

```

Hope that helps,  
-Jon

---

<div class="post-metadata">

**Author:** ![conighion](https://avatars.discourse-cdn.com/v4/letter/c/90db22/32.png) [@conighion](https://community.plotly.com/u/conighion)\
**Post date:** [January 15, 2021, 11:16am UTC](https://community.plotly.com/t/order-of-elements-in-plot/25011/3 "2021-01-15T11:16:53Z")

</div>

Hi @cvahlen

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

```auto
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”.

---

<div class="post-metadata">

**Author:** ![tobsam](https://avatars.discourse-cdn.com/v4/letter/t/edb3f5/32.png) [@tobsam](https://community.plotly.com/u/tobsam)\
**Post date:** [January 13, 2023, 2:13pm UTC](https://community.plotly.com/t/order-of-elements-in-plot/25011/4 "2023-01-13T14:13:25Z")

</div>

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