Hello,
I have a time series plot which plots multiple lines over time - example code:
fig = px.line(df_viz.query("publisher=='NYT'"), x="publication_date", y="proportion", color="query", title="Publisher: NYT")
Every aspect of this plot except the x value (date) gets manipulated via ipywidgets.
I would like to add some annotations for specific events. So I have a dataframe with 3 columns: date, description, and type. I would like to have a line spanning the full height of the plot for each event at the correct date, color-coded by the event type, and with the description displaying as hovertext/tooltip.
I have tried a few approaches. Drawing rectangles for each event is extremely verbous and tedious, and is not sustainable if I want to add many new events - plus, shapes do not have hovertext. I also tried to add the events as a bar chart using
fig_bar = px.bar(events, x="date", color="type", hover_data=["date", "event"])
fig.add_trace(fig_bar.data[0])
However, I cannot figure out how to make the y axis be the maximum y value (so each bar extends to the full height of the chart) and how to increase the width of each bar (they are essentially invisible in the full view of the chart, as each line represents one day, and the timeline is multiple decades long).
In case it is useful, I tried the shapes approach with two test events (using code from the documentation):
fig.update_layout(
shapes=[
go.layout.Shape(
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="2007-03-01",
y0=0,
x1="2007-03-30",
y1=1,
fillcolor="LightSalmon",
opacity=0.5,
layer="below",
line_width=0,
),
go.layout.Shape(
type="rect",
xref="x",
yref="paper",
x0="2017-04-01",
y0=0,
x1="2017-04-30",
y1=1,
fillcolor="LightSalmon",
opacity=0.5,
layer="below",
line_width=0,
)
]
)
I want to be able to visually see how my lines change before/after these events, and I want to be able to see the full description of the events on hover (so the text doesnβt crowd the plot). How can I solve this? Thanks in advance.