回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我试图从<a href="http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/" rel="noreferrer">Jake Vanderplas</a>中保存一个简单的matplotlib动画,但是我一直得到<code>OSError: [Errno 13] Permission denied</code>。</p>
<p>我应该注意到我对Jake Vanderplas的例子做了两个小的修改。我从MacPorts安装了ffmpeg,所以添加了<code>plt.rcParams['animation.ffmpeg_path'] = '/opt/local/bin'</code>行,遇到了(<a href="https://stackoverflow.com/questions/20137792/using-ffmpeg-and-ipython/20395798#20395798">Using FFmpeg and IPython</a>)中讨论的问题,所以添加了<code>FFwriter = animation.FFMpegWriter()</code>。</p>
<p>代码如下:</p>
<pre><code>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'])
</code></pre>
<p>这是回溯:</p>
<pre><code>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
</code></pre>
<p>我也尝试过使用Spyder的内置python,并收到了类似的回溯。有什么建议吗?</p>
<hr/>
<p>编辑:我意识到我没有给出正确的ffmpeg路径。显然,<code>plt.rcParams['animation.ffmpeg_path']</code>的工作方式与<code>PYTHONPATH</code>不同。必须使用<code>plt.rcParams['animation.ffmpeg_path'] = '/opt/local/bin/ffmpeg'</code>准确地告诉动画模块ffmpeg的位置。</p>
<p>现在,我得到一个电影文件,将播放,但内容是完全混乱。我不知道我在看什么。</p>
<p>这是回溯:</p>
<pre><code>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>
</code></pre>
<hr/>
<p>编辑:
不知为什么,现在一切都很好。我在家用电脑和工作电脑上试过一些东西,但是在我修复了ffmpeg路径问题之后,没有人能重新创建我得到的混乱的视频文件。</p>
<hr/>
<p>编辑:
啊哈!我找到了这个混蛋。有时我会导入一个包含<code>plt.rcParams['savefig.bbox'] = 'tight'</code>的模块。(我永远不会使用该模块,但rcParams会一直存在,直到重新启动python解释器。)该设置会导致视频全部乱码。我将在下面发布我的解决方案。</p>