我在保存matplotlib動畫時遇到了問題。當我執行下面的測試腳本:在Linux中保存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工具。我試過這個,發現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。謝謝!
你所看到的第一個錯誤是由matplotlib中的[this bug](https://github.com/matplotlib/matplotlib/issues/2651)造成的。它在v1.4.0中修復了,所以你應該考慮更新。 –
不確定你的意思是*「ffmpeg不可用」* - 你是否試過'sudo apt-get install ffmpeg'? –
嗨,是的,我做了'sudo apt-get install ffmpeg'並得到了'Package ffmpeg不可用,但是被其他包引用。 這可能意味着包丟失,已過時,或 只能從其他來源 E:包「的ffmpeg」沒有安裝候選人 ' –