Is there some way to call matplotlib.animation.FuncAnimation more than once in the same code? I have a list of numpy arrays. The arrays each contain a different number of coordinates. I want to loop through the list and animate plotting the points in each array.
Is there anything I can do to achieve these two different scenarios: 1. Retain the last frame from the previous animation and start a new animation over that. 2. Get rid of the last frame from the previous animation, and start a fresh animation, but keep the same layout set at the beginning of the code (including background plots).
When I try to use FuncAnimation in a for loop like below, only the first array gets animated. In between each array, I also need to do some other stuff, so I can't just get rid of the for loop and merge the arrays to be animated all together.
import numpy as np
import matplotlib.animation as animation
import matplotlib.pyplot as plt
mylines = [np.array([[1,2], [3,4]]), np.array([[5,6], [7,8], [9,10]])]
fig, ax = plt.subplots()
ax.set_xlim([-10, 10])
ax.set_ylim([-10, 10])
# There's some code here to set the layout (some background plots etc)
# I omitted it in this question
def update_plot(i, data, myplot):
myplot.set_data(data[:i, 0], data[:i, 1])
return myplot,
myplot, = ax.plot([], [], '.', markersize=5)
for k in xrange(len(mylines)):
data = mylines[k]
numframes = data.shape[0]+1
delay = 500
ani = animation.FuncAnimation(fig, update_plot, frames=numframes,
fargs=(data, myplot), interval=delay,
blit=True, repeat=False)
plt.show()
# Do some more stuff in the for loop here, that's not related to
# animations but related to the current array
# Then I want to retain the last frame from the animation, and start a new
# animation on top of it. Also, what should I do if I want to erase the
# last frame instead (erase only the points plotted by the FuncAnimation,
# but keep the layout I set at the beginning)
*Edited code because it had some syntax errors
Also tried following this question's solution:
ani = []
for k in xrange(len(mylines)):
data = mylines[k]
numframes = data.shape[0]+1
delay = 100
ani.append(animation.FuncAnimation(fig, update_plot, frames=numframes,
fargs=(data, myplot), interval=delay,
blit=True, repeat=False))
plt.show()
but it plots all the arrays all at once, with some weird flickering.