无法保存matplotlib动画

use*_*187 10 python animation matplotlib

我正在使用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()
Run Code Online (Sandbox Code Playgroud)

我没有遇到动画热图; 但是,当我尝试保存动画时,我遇到了一个问题

/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'
Run Code Online (Sandbox Code Playgroud)

任何人都知道问题是什么以及如何解决它?

编辑:问题是我没有安装ffmpeg.简单的brew安装允许代码工作

小智 10

我在这里找到了一个解决方案:http: //nbviewer.ipython.org/github/jrjohansson/scientific-python-lectures/blob/master/Lecture-4-Matplotlib.ipynb

基本上你需要ffmpeg库o较新的libav-tools

所以打开终端并以root身份输入

apt-get install ffmpeg
Run Code Online (Sandbox Code Playgroud)

要么

apt-get install libav-tools
Run Code Online (Sandbox Code Playgroud)

希望它可能有所帮助.