I'm stuck on animation.save. I cannot seem to save two animations into a
single output file using the keyword extra_anim. It only saves the second of
the two animations, even though each animation can be saved correctly to its
own file.
I've tried several different combinations of FuncAnimation and
ArtistAnimation for the two animations, but all failed. Below is a very
minimal example that illustrates the error. I'm using a Linux computer with
version 1.5.1 of matplotlib installed. ffmpeg is the default
animation.writer and mpeg4 is the default animation.codec.
I should also say that I'm fairly new to python (1+ years), but very new to
its animation. I have, however, spent considerable time researching this
problem on the internet. And I've also read the chapter on animations in Ben
Root's book.
Any help would be much appreciated.
···
-------------------
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
# initialize
xdata = np.array([0.2, 0.4, 0.6, 0.8])
ydata = np.array([0, 0, 0, 0])
anims = [] # list of animations
# set up figure
fig = plt.figure(figsize=(8,1.25))
ax = fig.add_subplot(111)
ax.set_xlim(0, 1)
ax.set_ylim(-0.5, 0.5)
# first animation, with FuncAnimation
line, = ax.plot([], [])
line, = ax.plot(xdata, ydata, 'ro')
def init():
line.set_data(xdata, ydata)
return line,
def animate(i): # update function
ax.figure.canvas.draw()
return line,
anim0 = animation.FuncAnimation(fig, animate, init_func=init, frames=100,
interval=20, blit=False)
anim0.save('anim0.mp4', extra_args=['-vcodec', 'libx264'])
# second animation, with ArtistAnimation
ax.set_xlim(0.5, 1)
frame_list = []
for i in range(100):
frame = ax.plot(xdata, ydata, 'ro')
frame_list.append(frame)
anim1 = animation.ArtistAnimation(fig, frame_list, interval=20,
repeat=False)
anim1.save('anim1.mp4', extra_args=['-vcodec', 'libx264'])
# combine the two animations
anims = [anim0, anim1]
anims[0].save('anims.mp4', extra_args=['-vcodec', 'libx264'],
extra_anim=anims[1:])
--
View this message in context: http://matplotlib.1069221.n5.nabble.com/Problem-with-animation-save-and-extra-anim-tp46844.html
Sent from the matplotlib - users mailing list archive at Nabble.com.