Plotly.py: feature request: z-ordering parameter for traces

Created on 2 Apr 2020  路  7Comments  路  Source: plotly/plotly.py

Hello,

it would be very nice to have the possibilty to set the z-ordering priority of traces in plotly. I think the way matplotlib handles it is pretty handy:

  • default z-order value for a trace is 0;
  • if multiple trace have the same z-order value, matplotlib decides the ordering of traces itself (random or whatever);
  • if a trace have a greater z-order than an other, it appears on top of it;
  • if a trace have a smaller z-order value than the other, it appears below;

Veeeery useful. Right now I am strugling to have a marker to appear on top of a bunch of line plots and it is very annoying.

Most helpful comment

Any news on this? This would be a great feature!

All 7 comments

By the way, the hack I found to overcome this problem right now is to store the trace I want to add on TOP in a globalvariable (on can think of a list if one have multiple trace to add in a certain ordering) and then, just before rendering the figure, I had the trace to the figure. Then the last added trace appears on top of all other traces.

I am encountering the same issue. I would like to add several lines on top of scatter plot generated by px.scatter() .I tried to reorder the traces by doing fig.data = fig.data[::-1] but it only changes the order shown in legend, I still have not been able to add lines on top of points after trying almost all the tricks online, which is a bit frustrating..

it would be super helpful if plotly can support something like zorder

Another use case for this feature is related to legend ordering. Currently, if you use the order of traces to specify the legend ordering, we cannot specify a z order independently. Since legend order is either None (i.e. insertion order) or alphabetical order, it is impossible to set an arbitrary order of legend items and at the same time set and arbitrary zorder of traces.

Has there been any movement to try to implement this?

A few notes on this:

  1. This would have to be implemented in Plotly.js rather than in Python
  2. Traces are currently drawn in batches by type, so scatter is always drawn on top of bar, etc, and this would be quite complicated to change, so the more straightforward option would be to control the other within types first

I should add that fig.data = fig.data[::-1] does work for me:

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

fig = px.scatter(x=[1,2,3,4], y=[1,2,3,4], size=[1,2,3,4], color=["a","a","b","b"])
fig.add_trace(go.Scatter(x=[1,2,3,4], y=[1,2,3,4], mode="lines", line_color="black"))
fig.data = fig.data[::-1]
fig.show()

as does reordering fig.data like this:

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

fig = px.scatter(x=[1,2,3,4], y=[1,2,3,4], size=[1,2,3,4], color=["a","a","b","b"])
fig.add_trace(go.Scatter(x=[1,2,3,4], y=[1,2,3,4], mode="lines", line_color="black"))
fig.data = (fig.data[1],fig.data[2],fig.data[0])
fig.show()

Any news on this? This would be a great feature!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dhirschfeld picture dhirschfeld  路  4Comments

jonmmease picture jonmmease  路  3Comments

astrojuanlu picture astrojuanlu  路  4Comments

keithjjones picture keithjjones  路  3Comments

entron picture entron  路  4Comments