无法使用FFMPEG保存matplotlib动画



我已经安装了ffmpeg并将其添加到路径上,并在命令提示符中进行了检查,但是我仍然无法保存动画。我已经尝试创建一个罪波,当我不尝试保存它时会动画,但是当我进行演示时会出现错误;

from __future__ import division
import numpy as numpy
from matplotlib import pyplot as pyplot
from matplotlib import animation
fig = pyplot.figure()
ax = pyplot.axes(xlim=(0, 2), ylim=(-2, 2))
line, = ax.plot([], [], lw=2)
def init():
    line.set_data([], [])
    return line,
def animate(i):
    x = numpy.linspace(0, 2, 1000)
    y = numpy.sin(2 * numpy.pi * (x - 0.01 * i))
    line.set_data(x, y)
    return line,
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=200, 
    interval=20, blit=True, repeat=False)
FFwriter = animation.FFMpegWriter()
anim.save('animation_testing.mp4', writer = FFwriter)
pyplot.show()

当我尝试运行它时,它会一遍又一遍地抛出相同的错误,我假设它在每个帧中迭代时;

Traceback (most recent call last):
  File "c:usersjamesappdatalocalenthoughtcanopyuserlibsite-
packagesmatplotlibbackendsbackend_wx.py", line 212, in _on_timer
    TimerBase._on_timer(self)
  File "c:usersjamesappdatalocalenthoughtcanopyuserlibsite-
packagesmatplotlibbackend_bases.py", line 1273, in _on_timer
    ret = func(*args, **kwargs)
  File "c:usersjamesappdatalocalenthoughtcanopyuserlibsite-
packagesmatplotlibanimation.py", line 910, in _step
    still_going = Animation._step(self, *args)
  File "c:usersjamesappdatalocalenthoughtcanopyuserlibsite-
packagesmatplotlibanimation.py", line 769, in _step
    self._draw_next_frame(framedata, self._blit)
  File "c:usersjamesappdatalocalenthoughtcanopyuserlibsite-
packagesmatplotlibanimation.py", line 787, in _draw_next_frame
    self._pre_draw(framedata, blit)
  File "c:usersjamesappdatalocalenthoughtcanopyuserlibsite-
packagesmatplotlibanimation.py", line 800, in _pre_draw
    self._blit_clear(self._drawn_artists, self._blit_cache)
  File "c:usersjamesappdatalocalenthoughtcanopyuserlibsite-
packagesmatplotlibanimation.py", line 840, in _blit_clear
    a.figure.canvas.restore_region(bg_cache[a])
KeyError: <matplotlib.axes._subplots.AxesSubplot object at 0x0000000009C04DD8>

由于它在_blit_clear中提到了一个错误,因此我尝试将闪光更改为False在Funcanimation中,但是当我不尝试保存时,它不会在pyplot.show()中进行动画。

我不确定错误的位置,因此无法解决该方法。

我正在使用Windows 10,Python 2.7.6和Matplotlib版本1.4.2

非常感谢!

添加;

pyplot.switch_backend('backend')

tkagg或qt4agg解决了我的问题。

感谢您为我解决这个问题的重要性!

最新更新