3

我正在尝试使用此处的示例使用 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.

现在,经过一番努力,我设置了一个FFMpegWriterusingpng而不是rgba,但现在我遇到了新错误......

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

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

4

2 回答 2

3

该 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'})
于 2013-05-23T18:39:19.990 回答
1

我不知道为什么,解决方案是让 matplotlib 使用一种叫做“Agg”的东西:

import matplotlib
matplotlib.use("Agg")
于 2013-02-02T00:33:12.633 回答