编程语言
首页 > 编程语言> > python-不支持带有matplotlib’格式“ rgba”的动画”

python-不支持带有matplotlib’格式“ rgba”的动画”

作者:互联网

我正在尝试使用示例from here使用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

我有一种印象,就是我没有安装所需的东西.一般而言,macports可以帮助我解决所有问题,而且我不确定要在哪里看.任何帮助将不胜感激.

解决方法:

那个use()语句对我没有帮助,但是当您尝试让basic matplotlib animation example的第二部分在IPython中工作时,我遇到了相同的错误.我使用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,animation,macports,python,macos
来源: https://codeday.me/bug/20191031/1974755.html