Hi @jhtong,
Your code is too dense, following this old example https://plotly.com/python/animations/#using-a-slider-and-buttons, that is not the best one to learn how to define an animation with slider.
As a rule, you have to define first fig.data
and then the frames
. In each frame definition you have to insert only the fig.data[0]
attributes that are changing from frame to frame
I modified your code to help understand better the steps to be completed to get an animation definition:
import plotly.graph_objects as go
import numpy as np
from plotly.offline import iplot
size = 10
theta = np.random.randint(17+1,size=size)*20*(3.14/180)
print(theta)
fig = go.Figure(go.Scatter(
marker= {'color': '#010101', 'size': 30, 'symbol': 'diamond'},
mode= 'markers',
name= '180.0',
text= 'Persoon',
x= [-0.99999873],
y=[0.00159265]))
# Define frames
frames = []
for k in range(len(theta)):
frames.append(go.Frame(data=
[go.Scatter(x = np.array(np.cos(theta[k])),
y = np.array(np.sin(theta[k])))
],
name= f'frame{k}'))
fig.frames = frames
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(steps = [dict(method= 'animate',
args= [[f'frame{k}'],
dict(mode= 'immediate',
frame= dict(duration=400, redraw=False),
transition=dict(duration= 0))
],
label=f'{k+1}'
) for k in range(len(theta))],
active=0,
transition= dict(duration= 0 ),
x=0, # slider starting position
y=0,
currentvalue=dict(font=dict(size=12),
prefix='frame: ',
visible=True,
xanchor= 'center'
),
len=1.0) #slider length
]
fig.update_layout(width=800, height=800,
xaxis_range = [-1, 1],
yaxis_range = [-1, 1],
updatemenus=updatemenus,
sliders=sliders)
iplot(fig)