保存matplotlib动画时遇到问题
我正在使用matplotlib来制作一个动态热图。我有一个文本文件(rs_h),里面有三列数据 - 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()
我在制作动态热图时没有遇到问题;但是,当我尝试保存动画时就遇到了麻烦。
/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'
有没有人知道问题出在哪里,以及如何解决它?
补充:问题是我没有安装ffmpeg。只需简单地运行一个brew install命令,就让代码正常工作了。
1 个回答
11
我找到了一种适用于Linux的解决办法,具体可以查看这个链接。基本上,你需要安装ffmpeg库或者libav-tools。
打开终端,然后以管理员身份输入以下命令:
apt-get install ffmpeg
或者你也可以输入:
apt-get install libav-tools
希望这些信息能对你有所帮助。