2

I am making a simple animated scatter plot in a Jupyter notebook, and I want it to repeat, but with a delay before it loops. This is supposed to be set with the repeat_delay parameter, but it has no effect when I use it in a Jupyter notebook using HTML(ani.to_html5_video()) to show the animation.

Here is a simple example of repositioning 20 points every 200 ms, but trying to add a 2 second delay before repeating the animation:

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
from IPython.display import HTML

frame_interval = 200
vid_repeat = True
repeat_delay = 2000  # set long so it would be obvious

def update_xy(i, scat):
    scat.set_offsets(np.random.random((20,2)))
    return scat,

fig = plt.figure()
init_data = np.random.random((20,3))
scat = plt.scatter(init_data[:,0], init_data[:,1], c=init_data[:,2], s=50, cmap = "hot")

ani = animation.FuncAnimation(fig, 
                              update_xy, 
                              frames=numframes,
                              interval = frame_interval,
                              repeat = vid_repeat,
                              repeat_delay = repeat_delay,
                              fargs=(scat,))
plt.close(ani._fig)
HTML(ani.to_html5_video())

At the end, it just loops around at frame_interval no matter what value I put for repeat_delay. I get the same result when I save the animation ani.save('foo.mp4'), or try to play it using HTML(ani.to_jshtml()).

Related Questions
Animation in iPython notebook
How to animate a scatter plot?

eric
  • 7,142
  • 12
  • 72
  • 138

0 Answers0