Hey guys, I’m new to plotly and still quite struggling with some of the syntax. I want to make the a plot with an animation button and a slider, based on a example code on the plotly website. The animation works without any problem, but I somehow can’t seem to fix the sliderbar. It won’t update (eg. show “Frame4” when the fifth frame of the animation is happening). Any ideas on where the problem may be?
import plotly.graph_objects as go
import numpy as np
from plotly.offline import plot
size = 10
theta = np.random.randint(17+1,size=size)*20*(3.14/180)
print(theta)
# Dictionary for the figure
fig_dict = {
"data": [],
"layout": {},
"frames": []
}
# Add the layout
fig_dict["layout"] = dict(
width=800,
height=800,
xaxis = dict(
range = [-1, 1],
),
yaxis = dict(
range = [-1, 1],
),
sliders = [dict(
args = ["transition", {
"duration": 400,
"easing": "cubic-in-out"
}],
initialValue = "0",
plotlycommand = "animate",
values = [i for i in range(len(theta))],
visible = True
)],
updatemenus = [dict(
buttons = [
dict(
args = [None, {"frame": {"duration": 500, "redraw": False},
"fromcurrent": True, "transition": {"duration": 300}}],
label = "Play",
method = "animate"
),
dict(
args = [[None], {"frame": {"duration": 0, "redraw": False},
"mode": "immediate",
"transition": {"duration": 0}}],
label = "Pause",
method = "animate"
)
],
direction = "left",
pad = {"r": 10, "t": 87},
showactive = False,
type = "buttons",
x = 0.1,
xanchor = "right",
y = 0,
yanchor = "top"
)]
)
sliders_dict = {
"active": 0,
"yanchor": "top",
"xanchor": "left",
"currentvalue": {
"font": {"size": 20},
"prefix": "Frame:",
"visible": True,
"xanchor": "right"
},
"transition": {"duration": 300, "easing": "cubic-in-out"},
"pad": {"b": 10, "t": 50},
"len": 0.9,
"x": 0.1,
"y": 0,
"steps": []
}
# Make each frame
for i in range(0,len(theta)-1):
frame = {"data": [], "name": "frame" +str(i)}
data_dict = dict(
x = np.array(np.cos(theta[i])),
y = np.array(np.sin(theta[i])),
mode = "markers",
text = "Persoon",
marker = dict(
color = "#010101",
symbol = "diamond",
size = 30),
name = theta[i]/(3.14/180)
)
if (i==0):
fig_dict["data"] = data_dict
else:
frame["data"] = data_dict
fig_dict["frames"].append(frame)
slider_step = dict(
args = [[theta[i]],{
"visible": True,
"frame": {"duration": 300, "redraw": False},
"mode": "immediate",
"transition": {"duration": 300}}],
label = str(i),
method = "animate"
)
sliders_dict["steps"].append(slider_step)
fig_dict["layout"]["sliders"] = [sliders_dict]
fig = go.Figure(fig_dict)
plot(fig)