【问题标题】:Unable to save Matplotlib animations using ffmpeg无法使用 ffmpeg 保存 Matplotlib 动画
【发布时间】:2018-06-05 18:38:42
【问题描述】:

我已经安装了 ffmpeg 并将其添加到路径中,并在命令提示符下检查了它是否有效,但我仍然无法保存动画。我尝试过创建一个 sin 波,当我不尝试保存它时它会产生动画,但在我做演示时会抛出错误;

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:\users\james\appdata\local\enthought\canopy\user\lib\site-
packages\matplotlib\backends\backend_wx.py", line 212, in _on_timer
    TimerBase._on_timer(self)
  File "c:\users\james\appdata\local\enthought\canopy\user\lib\site-
packages\matplotlib\backend_bases.py", line 1273, in _on_timer
    ret = func(*args, **kwargs)
  File "c:\users\james\appdata\local\enthought\canopy\user\lib\site-
packages\matplotlib\animation.py", line 910, in _step
    still_going = Animation._step(self, *args)
  File "c:\users\james\appdata\local\enthought\canopy\user\lib\site-
packages\matplotlib\animation.py", line 769, in _step
    self._draw_next_frame(framedata, self._blit)
  File "c:\users\james\appdata\local\enthought\canopy\user\lib\site-
packages\matplotlib\animation.py", line 787, in _draw_next_frame
    self._pre_draw(framedata, blit)
  File "c:\users\james\appdata\local\enthought\canopy\user\lib\site-
packages\matplotlib\animation.py", line 800, in _pre_draw
    self._blit_clear(self._drawn_artists, self._blit_cache)
  File "c:\users\james\appdata\local\enthought\canopy\user\lib\site-
packages\matplotlib\animation.py", line 840, in _blit_clear
    a.figure.canvas.restore_region(bg_cache[a])
KeyError: <matplotlib.axes._subplots.AxesSubplot object at 0x0000000009C04DD8>

由于它在 _blit_clear 中提到了一个错误,我尝试在 FuncAnimation 中将 blit 更改为 False,但是当我没有尝试保存时,它不会在 pyplot.show() 中设置动画。

我不确定错误可能出在哪里,因此无法解决此问题。

我使用的是 Windows 10、python 2.7.6 和 matplotlib 1.4.2 版

非常感谢!

【问题讨论】:

  • 这似乎与 ffmpeg 完全无关。这是一个抛出的 matplotlib 错误。这很可能是一个错误。您正在使用不常用的 wx 后端。使用“TkAgg”或“Qt4Agg”作为后端,我在运行上面没有任何问题,无论是否使用 blitting。在任何情况下,您都应该使用您可以提供的有关您的系统、操作系统、python 和 matplotlib 版本、如何/在何处运行它的尽可能多的信息来更新问题。
  • 我已经改变了我的后端,现在它运行没有错误,但是当完成后 mp4 文件仍然不存在,所以这是一个新的错误。不过,我想这是进步,我永远不会知道如何更改后端!我还按照您的建议更新了更多系统详细信息。
  • 你的 matplotlib 版本比较旧,如果你想让它与 wx 后端一起工作,我建议先更新到最新的 matplotlib。如果使用 wx 后端错误仍然存​​在,您可以提交错误报告。关于没有文件出现的问题,这宁愿与 ffmpeg 有关 - 但是如果不能产生输出(也许它只是在不同的文件夹中?)应该返回一些消息(也许它只是在不同的文件夹中?)你可以尝试一些可能的参数 @ 987654323@,也可以试试FFMpegFileWriter

标签: python animation matplotlib ffmpeg


【解决方案1】:

添加;

pyplot.switch_backend('backend')

后端是 TkAgg 或 Qt4Agg 解决了我的问题。

感谢 ImportanceOfBeingErnest 为我解决这个问题!

【讨论】:

    猜你喜欢
    • 2014-05-29
    • 2015-09-20
    • 2014-06-18
    • 2020-07-12
    • 2014-07-14
    • 2017-09-16
    • 2018-12-15
    • 1970-01-01
    • 2013-08-03
    相关资源
    最近更新 更多