Python 无法保存 matplotlib 动画
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23856990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't save matplotlib animation
提问by renger
I am trying to get a simple animation saved using ffmpeg. I followed a tutorial to install ffmpeg, and I can now access it from the command prompt.
我正在尝试使用 ffmpeg 保存一个简单的动画。我按照教程安装了 ffmpeg,现在我可以从命令提示符访问它。
Now I run this piece of code:
现在我运行这段代码:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
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)
mywriter = animation.FFMpegWriter()
anim.save('mymovie.mp4',writer=mywriter)
plt.show()
I get this error:
我收到此错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 523, in runfile
execfile(filename, namespace)
File "C:\Users\Renger\.xy\startups\b.py", line 23, in <module>
anim.save('mymovie.mp4',writer=mywriter)
File "C:\Python27\lib\site-packages\matplotlib\animation.py", line 609, in save
with writer.saving(self._fig, filename, dpi):
File "C:\Python27\lib\contextlib.py", line 17, in __enter__
return self.gen.next()
File "C:\Python27\lib\site-packages\matplotlib\animation.py", line 166, in saving
self.setup(*args)
File "C:\Python27\lib\site-packages\matplotlib\animation.py", line 156, in setup
self._run()
File "C:\Python27\lib\site-packages\matplotlib\animation.py", line 183, in _run
stdin=subprocess.PIPE)
File "C:\Python27\lib\subprocess.py", line 711, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 948, in _execute_child
startupinfo)
WindowsError: [Error 2] Het systeem kan het opgegeven bestand niet vinden
The last dutch sentence does mean something like: The system can't find the specified file.
最后一句荷兰语的意思是这样的:系统找不到指定的文件。
What do these errors mean, and how can I solve them?
这些错误是什么意思,我该如何解决?
采纳答案by Padraic Cunningham
You need to specify your path to ffmpeg
:
您需要指定您的路径ffmpeg
:
On linux I use:
在 linux 上我使用:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
plt.rcParams['animation.ffmpeg_path'] = '/usr/bin/ffmpeg'
You will obviously have to point to your windows .exe instead of '/usr/bin/ffmpeg'
您显然必须指向您的 windows .exe 而不是 '/usr/bin/ffmpeg'
If you don't have ffmpeg
installed, you can get it here
如果你还没有ffmpeg
安装,你可以在这里得到它
回答by Mark
Line 183 in animation.py is the subprocess.Popen
call to ffmpeg
. It seems that the ffmpeg
exe is not where matplotlib expects it to be.
animation.py 中的第 183 行是subprocess.Popen
对ffmpeg
. 似乎ffmpeg
exe 不是 matplotlib 期望的位置。
My first attempt would be to put the install path (directory) to ffmpeg
into the windows Path
environmental variable. I'm guessing that animation.py is expecting it to be available globally (as it would be under Linux).
我的第一次尝试是将安装路径(目录)ffmpeg
放入 windowsPath
环境变量中。我猜 animation.py 期望它在全球范围内可用(就像在 Linux 下一样)。
If that doesn't work, I'd inspect the subprocess.Popen
call in animation.py to see exactly what it is doing. You could break point it or adjust the verbose.report variable in your matplotlibrcfile to spit it out. Line 179 there is:
如果这不起作用,我会检查subprocess.Popen
animation.py 中的调用以查看它在做什么。您可以断点它或调整matplotlibrc文件中的 verbose.report 变量以将其吐出。第179行有:
verbose.report('MovieWriter.run: running command: %s' %
' '.join(command))
回答by Padraic Cunningham
for some animation "anim" i use on windows:
对于我在 Windows 上使用的一些动画“动画”:
plt.rcParams['animation.ffmpeg_path'] ='E:\Media\ffmpeg\bin\ffmpeg.exe'
FFwriter = animation.FFMpegWriter()
anim.save('basic_animation.mp4', writer = FFwriter, fps=30)
where path should be with \ \ and not with / or \ between folders
其中路径应该与 \\ 而不是与 / 或 \ 文件夹之间
回答by CFDace
On Windows, I had to use
plt.rcParams['animation.ffmpeg_path'] = 'C:\\mypath'
with all double '\' in the path and this line has to be mentioned before
在 Windows 上,我必须plt.rcParams['animation.ffmpeg_path'] = 'C:\\mypath'
在路径中使用
所有双 '\' 并且必须在此之前提到这一行
from matplotlib import animation
and mypath = path of ffmpeg.exe on the local disk.
和 mypath = 本地磁盘上 ffmpeg.exe 的路径。
Additionally, I also added the ffmpeg path in the environment variables.
此外,我还在环境变量中添加了 ffmpeg 路径。
It took me two days and after going through tons of advice I was able to solve this issue of saving animation.
我花了两天时间,在听取了大量建议后,我终于解决了保存动画的问题。
Thank you everyone for all the answers.
谢谢大家的回答。