【发布时间】:2014-05-29 06:17:56
【问题描述】:
我正在尝试从 Jake Vanderplas 保存一个简单的 matplotlib 动画,但我不断收到 OSError: [Errno 13] Permission denied。
我应该注意我对 Jake Vanderplas 的示例做了两个小的修改。我从 MacPorts 安装了 ffmpeg,所以我添加了 plt.rcParams['animation.ffmpeg_path'] = '/opt/local/bin' 行,遇到了 (Using FFmpeg and IPython) 中讨论的问题,所以我添加了 FFwriter = animation.FFMpegWriter()。
代码如下:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
plt.rcParams['animation.ffmpeg_path'] = '/opt/local/bin'
fig = plt.figure()
ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))
line, = ax.plot([], [], lw=2)
def init():
line.set_data([], [])
return line,
def animate(i):
x = np.linspace(0, 2, 1000)
y = np.sin(2 * np.pi * (x - 0.01 * i))
line.set_data(x, y)
return line,
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=200, interval=20, blit=True)
FFwriter = animation.FFMpegWriter()
anim.save('basic_animation.mp4', writer = FFwriter, fps=30, extra_args=['-vcodec', 'libx264'])
这是回溯:
File "ani_debug.py", line 34, in <module>
anim.save('basic_animation.mp4', writer = FFwriter, fps=30, extra_args=['-vcodec', 'libx264'])
File "/Users/Ben/Library/Enthought/Canopy_64bit/User/lib/python2.7/site- packages/matplotlib/animation.py", line 712, in save
with writer.saving(self._fig, filename, dpi):
File "/Applications/Canopy.app/appdata/canopy-1.3.0.1715.macosx-x86_64/Canopy.app/Contents/lib/python2.7/contextlib.py", line 17, in __enter__
return self.gen.next()
File "/Users/Ben/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/animation.py", line 169, in saving
self.setup(*args)
File "/Users/Ben/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/animation.py", line 159, in setup
self._run()
File "/Users/Ben/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/animation.py", line 186, in _run
stdin=subprocess.PIPE)
File "/Applications/Canopy.app/appdata/canopy-1.3.0.1715.macosx-x86_64/Canopy.app/Contents/lib/python2.7/subprocess.py", line 709, in __init__
errread, errwrite)
File "/Applications/Canopy.app/appdata/canopy-1.3.0.1715.macosx-x86_64/Canopy.app/Contents/lib/python2.7/subprocess.py", line 1326, in _execute_child
raise child_exception
OSError: [Errno 13] Permission denied
我也尝试过使用 Spyder 的内置 python 并收到类似的回溯。有什么建议吗?
编辑:我意识到我没有给出 ffmpeg 的正确路径。显然,plt.rcParams['animation.ffmpeg_path'] 与PYTHONPATH 的工作方式不同。你必须用plt.rcParams['animation.ffmpeg_path'] = '/opt/local/bin/ffmpeg'告诉动画模块ffmpeg在哪里。
现在,我得到一个可以播放的电影文件,但内容完全是乱码。我不知道我在看什么。
这是回溯:
Exception in Tkinter callback
Traceback (most recent call last):
File "Tkinter.pyc", line 1470, in __call__
File "Tkinter.pyc", line 531, in callit
File "/Applications/Spyder.app/Contents/Resources/lib/python2.7/matplotlib/backends/backend_tkagg.py", line 141, in _on_timer
TimerBase._on_timer(self)
File "/Applications/Spyder.app/Contents/Resources/lib/python2.7/matplotlib/backend_bases.py", line 1203, in _on_timer
ret = func(*args, **kwargs)
File "/Applications/Spyder.app/Contents/Resources/lib/python2.7/matplotlib/animation.py", line 876, in _step
still_going = Animation._step(self, *args)
File "/Applications/Spyder.app/Contents/Resources/lib/python2.7/matplotlib/animation.py", line 735, in _step
self._draw_next_frame(framedata, self._blit)
File "/Applications/Spyder.app/Contents/Resources/lib/python2.7/matplotlib/animation.py", line 753, in _draw_next_frame
self._pre_draw(framedata, blit)
File "/Applications/Spyder.app/Contents/Resources/lib/python2.7/matplotlib/animation.py", line 766, in _pre_draw
self._blit_clear(self._drawn_artists, self._blit_cache)
File "/Applications/Spyder.app/Contents/Resources/lib/python2.7/matplotlib/animation.py", line 806, in _blit_clear
a.figure.canvas.restore_region(bg_cache[a])
KeyError: <matplotlib.axes.AxesSubplot object at 0x104cfb150>
编辑: 出于某种原因,现在一切正常。我在我的家用电脑和工作电脑上都试过了,在我修复了 ffmpeg 路径问题后,没有人可以重新创建我得到的乱码视频文件。
编辑:
啊啊啊!我追踪到了这个傻逼。有时我会导入一个包含plt.rcParams['savefig.bbox'] = 'tight' 的模块。 (我永远不会使用该模块,但 rcParams 会一直存在,直到您重新启动 python 解释器。)该设置会导致视频出现乱码。我将在下面发布我的解决方案。
【问题讨论】:
-
等等.. 现在你得到了回溯但也得到了输出?
-
好吧,粗鲁。我刚刚在我的工作计算机上尝试了上面的代码(使用正确的 ffmpeg 路径),它运行良好。在此之前我的所有测试都是在我的家用电脑上进行的。也许我需要在家里的电脑上重新安装 ffmpeg。
标签: python animation matplotlib