gpt4 book ai didi

python - 在 Linux 中保存 matplotlib 动画

转载 作者:太空狗 更新时间:2023-10-29 11:12:57 25 4
gpt4 key购买 nike

我在保存 matplotlib 动画时遇到问题。当我执行以下测试脚本时:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_ylim([0,11])
ax.set_xlim([0,100])

u, v, ims = [], [], []
u.append(0)
v.append(10)
for i in range(100):
u.append(i+1)
v.append(10)
ims.append(ax.plot(u, v, 'b-', linewidth=3.))

im_ani = animation.ArtistAnimation(fig, ims, interval=50, repeat_delay=3000,
blit=True)

im_ani.save('c.mp4')

我收到以下错误:

im_ani.save('c.mp4')   
File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 712, in save
with writer.saving(self._fig, filename, dpi):
AttributeError: 'str' object has no attribute 'saving'

现在根据这个answer ,我需要安装 ffmpeg 或 libav-tools。我尝试了这个,发现 ffmpeg 不可用,但是 libav-tools 似乎安装正确。但是,当我再次执行我的脚本时,我仍然得到和以前一样的错误。

我也(按照 this 回答的建议)尝试做

mywriter = animation.FFMpegWriter()
anim.save('mymovie.mp4',writer=mywriter)

但这也不管用!它导致了以下错误:

  File "anitest.py", line 22, in <module>
im_ani.save('mymovie.mp4',writer=mywriter)
File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 712, in save
with writer.saving(self._fig, filename, dpi):
File "/usr/lib/python2.7/contextlib.py", line 17, in __enter__
return self.gen.next()
File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 169, in saving
self.setup(*args)
File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 159, in setup
self._run()
File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 186, in _run
stdin=subprocess.PIPE)
File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1327, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory

如有任何帮助,我们将不胜感激。我正在使用 Ubuntu 14.04。谢谢!

最佳答案

我们在上面的评论中找到了解决方案。总结:

  • 出现那个相当神秘的错误消息的原因:

    AttributeError: 'str' object has no attribute 'saving'

    this bug in matplotlib,已在版本 1.4.0 ( also mentioned here ) 中修复。

  • 但是,将 matplotlib 更新到 1.4.0 或更新版本不会解决问题的根本原因,这只是未安装 ffmpeg(see here ).

  • OP 在安装 ffmpeg 时遇到困难,因为它是 dropped from the official Ubuntu repositories in version 14.04(已在 Ubuntu 15.04 中恢复)。对于那些仍在使用旧版本 Ubuntu 的人来说,一种解决方法是添加 this unofficial PPA :

    sudo add-apt-repository ppa:mc3man/trusty-media
    sudo apt-get update
    sudo apt-get dist-upgrade # recommended on first use
    sudo apt-get install ffmpeg

关于python - 在 Linux 中保存 matplotlib 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34383690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com