matplotlib animation produces a blank

2019-09-02 12:49发布

问题:

I have a bunch of PNGs that I want to turn into an animation. I use matplotlib for this. I can display the resulting animation on the screen and everything looks fine. But the saved MP4 is just a blank. I mean I can play it, but it just shows a white, featureless window. Any ideas what I'm doing wrong? Here's the code:

import matplotlib
matplotlib.use('TKAgg')
import pylab as pyl
import matplotlib.pyplot as pplt
import matplotlib.image as mplimg
import matplotlib.animation as mplanim

myimages = []

for k in range(1,100):
    fname = "data{0:03d}.png".format(k)
    img = mplimg.imread(fname)
    imgplot = pplt.imshow(img)
    myimages.append([imgplot])

fig = pyl.figure()

myanim = mplanim.ArtistAnimation(fig, myimages, interval=20,
                                  blit=True, repeat_delay=1000)


myanim.save("anim.mp4", fps=10)

pyl.show()

UPDATE: Moving fig = pyl.figure() to the top of the code, right after the imports solves the problem. If anyone knows why, feel free to tell! Thanks.

回答1:

Correct this line:

myimages.append([imgplot])

into:

myimages.append(imgplot)

and see if it works.