0

I'd like to stack a plotly.express line plot atop a filled rectangle from graph_objects. Creating the figure with px.line and then adding the filled rectangle makes the rectangle show up above the line (notebook).

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

fig = px.line(x=[0, 2, 4], y=[1, 2, 3])
fig.add_trace(go.Scatter(
    x=[1, 1, 3, 3, 1], 
    y=[0, 5, 5, 0, 0],
    fill="toself")
)
fig.show()

plot with

How can I get the line above the rectangle?

Max Ghenis
  • 14,783
  • 16
  • 84
  • 132

1 Answers1

0

Applying the answer to How to add traces in plotly.express :

fig = go.Figure()
fig.add_trace(go.Scatter(
    x=[1, 1, 3, 3, 1], 
    y=[0, 5, 5, 0, 0],
    fill="toself")
)
fig.add_traces(list(px.line(x=[0, 2, 4], y=[1, 2, 3]).select_traces()))
fig.show()

enter image description here

Max Ghenis
  • 14,783
  • 16
  • 84
  • 132