Python 无法保存 matplotlib 动画
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24815717/
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
Trouble saving matplotlib animation
提问by user3851187
I'm using matplotlib to make an animated heatmap. I have data in a text file (rs_h) with 3 columns - x, y, z; i'm using scatterplot to make a simple heatmap, and then using the animation package to update the heatmap over time
我正在使用 matplotlib 制作动画热图。我的文本文件 (rs_h) 中有 3 列数据 - x、y、z;我正在使用散点图制作一个简单的热图,然后使用动画包随着时间的推移更新热图
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
data = pd.read_table('rs_h', header=None, sep=r"\s*")
frames = np.array_split(data, 9)
def main():
numframes = 9
numpoints = 75
x, y, c = np.random.random((3, numpoints))
fig = plt.figure()
scat = plt.scatter(x, y, c=c)#, s=100)
ani = animation.FuncAnimation(fig, update_plot, frames=xrange(numframes),
interval = 5)
#ani.save("movie.avi", codec='avi')
plt.show()
def update_plot(i):
frame = frames[i]
scat = plt.scatter(frame[0], frame[1], c=frame[2])
return scat,
main()
I'm having no trouble getting the animated heatmap; however, i run into an issue when i try to save the animation
我可以轻松获取动画热图;但是,当我尝试保存动画时遇到了问题
/Users/Arjun/anaconda/lib/python2.7/site-packages/matplotlib/animation.py:695: UserWarning: MovieWriter ffmpeg unavailable
warnings.warn("MovieWriter %s unavailable" % writer)
Traceback (most recent call last):
File "heat_ani.py", line 29, in <module>
main()
File "heat_ani.py", line 21, in main
ani.save("movie.avi", codec='avi')
File "/Users/Arjun/anaconda/lib/python2.7/site-packages/matplotlib/animation.py", line 712, in save
with writer.saving(self._fig, filename, dpi):
AttributeError: 'str' object has no attribute 'saving'
Anyone know what the issue is and how to get around it?
任何人都知道问题是什么以及如何解决它?
EDIT: The issue was that i didn't have ffmpeg installed. A simple brew install allowed the code to work
编辑:问题是我没有安装 ffmpeg。一个简单的 brew install 允许代码工作
回答by jordi
I found a solution for Linux. Basically you need ffmpeg library or libav-tools
我找到了Linux的解决方案。基本上你需要 ffmpeg 库或 libav-tools
Open terminal and type as root
打开终端并以 root 身份输入
apt-get install ffmpeg
or
或者
apt-get install libav-tools
Hope it might help.
希望它可能会有所帮助。