@nicolaskruchten @empet @Emmanuelle sorry that mentioning all of you as I am not sure who will ends up handling this post. Following an example in the forum, I am trying to add annotations to be updated with slider.
It shows the initial annotations correctly however it does not update with sliding the step. My code is attached below and let me know if I did anything wrong. As annotations is in the layout and I have used similar specification as title, but it does not work. Thanks for any feedback in advance.
import plotly.graph_objects as go
import numpy as np
# Create figure
fig = go.Figure()
annotations_dict=[]
# Add traces, one for each slider step
for step in np.arange(0, 2, 0.1):
x_vec=np.arange(0, 1, 0.1)
y_vec=np.sin(step * np.arange(0, 1, 0.1))
fig.add_trace(
go.Scatter(
visible=False,
line=dict(color="#00CED1", width=6),
name="π = " + str(step),
x=x_vec,
y=y_vec))
annotations=[]
for n in range(len(y_vec)):
record = go.layout.Annotation(
text=str("{:0.2f}".format(y_vec[n])),
y=y_vec[n],
x=x_vec[n],
yref="y1",
xref="x1",
font=dict(color='#484848', size=12),
showarrow=False,
)
annotations.append(record)
# print(record)
annotations_dict.append(annotations)
# Make 10th trace visible
fig.data[10].visible = True
fig.update_layout(
title="Frequency: Step-"+str(10),
annotations=annotations_dict[10],
)
# Create and add slider
steps = []
for i in range(len(fig.data)):
step = dict(
method="update",
args=[
{"visible": [False] * len(fig.data)}, # update for traces
{"title": "Frequency: Step-"+str(i)},
{"annotations": annotations_dict[i]},
# {"annotations": [False] * len(annotations_dict)}, # update for layout
],
)
step["args"][0]["visible"][i] = True # Toggle i'th trace to "visible"
# step["args"][2]["annotations"][i] = True # Toggle i'th annotation to "visible"
steps.append(step)
sliders = [dict(
active=10,
currentvalue={"prefix": "Frequency: "},
pad={"t": 50},
steps=steps
)]
fig.update_layout(
sliders=sliders
)
fig.show()