So I have this data series that has a binary recession variable and I would like to create rectangular highlights, just like these: https://plot.ly/python/shapes/#highlighting-time-series-regions-with-rectangle-shapes. I am new to plotly and cannot figure out how to do this. Any tipps would be much appreciated. Thank you.
Hi @fkpisk welcome to the forum! What exactly canβt you adapt from the linked example to your use case?
More generally, if you are new to plotly I recommend reading through the following doc pages
and to take a quick look at https://plot.ly/python/plotly-fundamentals/ and https://plot.ly/python/#basic-charts
1 Like
Hi @fkpisk
For an automatic creation of rectangular shapes to highlight some time-intervals on the graph, you can define a function that returns the filled rectangle, instead of defining explicitly each shape, as in the example at the link you pasted here:
import plotly.graph_objects as go
import pandas as pd
def get_highliting_rectangle(start_date, end_date, fillcolor='blue', opacity=0.25):
return dict(
type="rect",
xref="x",
yref="paper",
x0=start_date,
y0=0,
x1=end_date,
y1=1,
fillcolor=fillcolor,
opacity=opacity,
layer="below",
line_width=0,
)
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
fig = go.Figure([go.Scatter(x=df['Date'], y=df['AAPL.High'])])
shapes = []
shapes.append(get_highliting_rectangle("2015-06-25", "2015-08-03"))
shapes.append(get_highliting_rectangle("2016-01-08", "2016-02-24"))
fig.update_layout(shapes=shapes, width=700, height=450)
2 Likes