Black Lives Matter. Please consider donating to Black Girls Code today.
https://www.blackgirlscode.com

Controlling animation speed using graph_objects in python

I am trying to generate an animated plot using following code.

trace1 = go.Scatter(y=[1], name='Testing Points')
layout =go.Layout(title='Animation Test',
                  title_x=0.5,
                  xaxis_title='Time', 
                  yaxis_title='Test Points',
                  updatemenus=[dict(type="buttons",
                                    buttons=[dict(label="Play",
                                                  method="animate",
                                                  args=[None])])]
                           )
frames= []
for i in range(0,100):
    trace1 = go.Scatter(y=list(range(i)))
    frames.append(go.Frame(data=[trace1]))
    
fig = go.Figure(data=[trace1], 
          layout=layout,
          frames=frames
         )

fig.show()

Can someone help me figure out how to control the animation speed of this? I have seen ‘transition/duration’ property, but not really sure how to use this in the above setting.

Hi @rahulrajpl,

This a modified version of your code that works:

y=np.arange(100)     
fig=go.Figure(go.Scatter(y=y[:1], mode='lines', name='Testing Points'))
fig.update_layout(title='Animation Test',
                  title_x=0.5,
                  width=600, height=600, 
                  xaxis_title='Time', 
                  yaxis_title='Test Points',
                  yaxis_range=(0,99),
                  xaxis_range=(0,99), #you generate y-values for i =0, ...99, 
                                      #that are assigned, by default, to x-values 0, 1, ..., 99
                  
                  updatemenus=[dict(buttons = [dict(
                                               args = [None, {"frame": {"duration": 50, 
                                                                        "redraw": False},
                                                              "fromcurrent": True, 
                                                              "transition": {"duration": 0}}],
                                               label = "Play",
                                               method = "animate")],
                                type='buttons',
                                showactive=False,
                                y=1,
                                x=1.12,
                                xanchor='right',
                                yanchor='top')])
                                          
                    
frames= [go.Frame(data=[go.Scatter(y=y[:i])]) for i in range(1, 100)]
fig.update(frames=frames)

fig.show()

The transition duration defines the amount of time spent interpolating a trace from one state to another (currently limited to scatter traces), while the frame duration defines the total time spent in that state, including time spent transitioning.

1 Like

Thank you. That worked.