Problem 1: images are not displayed
You need to save the animation object in a variable:
my_anim = animation.ArtistAnimation(fig, myimages, interval=100)
This requirement is specific to animationand is not consistent with another graphing function in matplotlib, where you can usually use my_plot=plt.plot()or plt.plot()evenly.
This issue is further discussed here .
Problem 2: Save does not work
animation . , save
ArtistAnimation. , , save animation, , .
3:
, . , plt.imshow(), , , , pyplot .
python fig = plt.figure(), ( ) " 2".
, .
:
import matplotlib.pyplot as plt
import matplotlib.image as mgimg
from matplotlib import animation
fig = plt.figure()
myimages = []
for p in range(1, 4):
fname = "heatflow%03d.png" %p
img = mgimg.imread(fname)
imgplot = plt.imshow(img)
myimages.append([imgplot])
my_anim = animation.ArtistAnimation(fig, myimages, interval=1000, blit=True, repeat_delay=1000)
plt.show()
( , 3 "heatflow001.png" "heatflow003.png".)
FuncAnimation
, , FuncAnimation, . ,
. , FuncAnimation . , , .
:
from matplotlib import pyplot as plt
from matplotlib import animation
import matplotlib.image as mgimg
import numpy as np
fig = plt.figure()
ax = plt.gca()
def init():
imobj.set_data(np.zeros((100, 100)))
return imobj,
def animate(i):
fname = "heatflow%03d.png" % i
img = mgimg.imread(fname)[-1::-1]
imobj.set_data(img)
return imobj,
imobj = ax.imshow( np.zeros((100, 100)), origin='lower', alpha=1.0, zorder=1, aspect=1 )
anim = animation.FuncAnimation(fig, animate, init_func=init, repeat = True,
frames=range(1,4), interval=200, blit=True, repeat_delay=1000)
plt.show()