使用 Matplotlib 的动画“不支持格式”RGBA”



我正在尝试使用matplotlib制作动画,使用此处的示例。 起初的问题是没有安装 ffmpeg,所以我从 macports 安装了它 - 但并不是说不支持"rgba"。 错误消息:

File "Plot_shocktube1D.py", line 85, in <module>
    animDensity.save(densityAnimationName, extra_args=['-vcodec', 'libx264']) #, writer=movWriter)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/animation.py", line 615, in save
    writer.grab_frame()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/animation.py", line 199, in grab_frame
    dpi=self.dpi)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/figure.py", line 1363, in savefig
    self.canvas.print_figure(*args, **kwargs)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/backend_bases.py", line 2012, in print_figure
    print_method = self._get_print_method(format)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/backend_bases.py", line 1953, in _get_print_method
    '%s.' % (format, ', '.join(formats)))
ValueError: Format "rgba" is not supported.
Supported formats: bmp, emf, eps, gif, jpeg, jpg, pdf, pgf, png, ps, raw, rgba, svg, svgz, tif, tiff.

现在,经过一番挣扎,我使用 png 而不是 rgba 设置了一个FFMpegWriter,但现在我遇到了新的错误......

movWriter = anim.FFMpegWriter(fps=5, codec=None, bitrate=None, extra_args=None, metadata=None)
movWriter.frame_format = 'jpg'
animDensity = anim.FuncAnimation(fig, animateDensity, frames=10, interval=100, blit=True)
animDensity.save(densityAnimationName, extra_args=['-vcodec', 'libx264'], writer=movWriter)

有错误:

File "Plot_shocktube1D.py", line 85, in <module>
    animDensity.save(densityAnimationName, extra_args=['-vcodec', 'libx264'], writer=movWriter)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/animation.py", line 615, in save
    writer.grab_frame()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/animation.py", line 199, in grab_frame
    dpi=self.dpi)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/figure.py", line 1363, in savefig
    self.canvas.print_figure(*args, **kwargs)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/backend_bases.py", line 2093, in print_figure
    **kwargs)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/backends/backend_macosx.py", line 326, in print_jpg
    self._print_bitmap(filename, *args, **kwargs)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/backends/backend_macosx.py", line 319, in _print_bitmap
    self.write_bitmap(filename, width, height, dpi)
ValueError: Unknown file type

我的印象是我只是没有安装我需要的东西...... 通常Macport为我设置了所有内容,我真的不确定该去哪里看。 任何帮助将不胜感激。

那个 use() 语句对我没有帮助,但我遇到了与您试图在 IPython 中获取基本 matplotlib 动画示例的第二部分相同的错误。 我使用 macports 安装 ffmpeg,然后收到相同的 rgba 不支持警告(尽管 rgba 出现在支持的格式列表中!

无论如何,我发现如果我使用明确的FFMpegFileWriter(不仅仅是FFMpegWriter),一切都很好,rgba问题消失了,我得到了一个可用的m4v文件:

import matplotlib.animation as animation
fig2 = plt.figure()
x = np.arange(-9, 10)
y = np.arange(-9, 10).reshape(-1, 1)
base = np.hypot(x, y)
ims = []
for add in np.arange(15):
    ims.append((plt.pcolor(x, y, base + add, norm=plt.Normalize(0, 30)),))
im_ani = animation.ArtistAnimation(fig2, ims, interval=50, repeat_delay=3000, blit=True)
im_ani.save('im.m4v', writer=animation.FFMpegFileWriter(), metadata={'artist':'Guido'})

我不知道为什么,解决方案是让matplotlib使用名为"Agg"的东西:

import matplotlib
matplotlib.use("Agg")

最新更新