Plotly Shapes - horizontal Rectangle Shapes

Below i can code for two vertical rectangle shapes.
How it should be modified in order to get a two horizontal rectangle shapes.
I have tried my best, but i can’t figure it out.

import plotly.graph_objects as go

fig = go.Figure()

Add scatter trace for line

fig.add_trace(go.Scatter(
x=[β€œ2015-02-01”, β€œ2015-02-02”, β€œ2015-02-03”, β€œ2015-02-04”, β€œ2015-02-05”,
β€œ2015-02-06”, β€œ2015-02-07”, β€œ2015-02-08”, β€œ2015-02-09”, β€œ2015-02-10”,
β€œ2015-02-11”, β€œ2015-02-12”, β€œ2015-02-13”, β€œ2015-02-14”, β€œ2015-02-15”,
β€œ2015-02-16”, β€œ2015-02-17”, β€œ2015-02-18”, β€œ2015-02-19”, β€œ2015-02-20”,
β€œ2015-02-21”, β€œ2015-02-22”, β€œ2015-02-23”, β€œ2015-02-24”, β€œ2015-02-25”,
β€œ2015-02-26”, β€œ2015-02-27”, β€œ2015-02-28”],
y=[-14, -17, -8, -4, -7, -10, -12, -14, -12, -7, -11, -7, -18, -14, -14,
-16, -13, -7, -8, -14, -8, -3, -9, -9, -4, -13, -9, -6],
mode=β€œlines”,
name=β€œtemperature”
))

Add shape regions

fig.update_layout(
shapes=[
# 1st highlight during Feb 4 - Feb 6
dict(
type=β€œrect”,
# x-reference is assigned to the x-values
xref=β€œx”,
# y-reference is assigned to the plot paper [0,1]
yref=β€œpaper”,
x0=β€œ2015-02-04”,
y0=0,
x1=β€œ2015-02-06”,
y1=1,
fillcolor=β€œLightSalmon”,
opacity=0.5,
layer=β€œbelow”,
line_width=0,
),
# 2nd highlight during Feb 20 - Feb 23
dict(
type=β€œrect”,
xref=β€œx”,
yref=β€œpaper”,
x0=β€œ2015-02-20”,
y0=0,
x1=β€œ2015-02-22”,
y1=1,
fillcolor=β€œLightSalmon”,
opacity=0.5,
layer=β€œbelow”,
line_width=0,
)
]
)

fig.show()

@pitS

The shapes with x0, x1 given as strings that represents dates do not work. These strings must be converted to datetime objects. See this example: https://chart-studio.plotly.com/~empet/15269/plotly-shapes-with-date-coordinates-def/#/.

1 Like

Thank you for the reply and for the link. It works now. :+1: