Trouble saving matplotlib animation [duplicate]

2020-06-03 04:42发布

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

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

1条回答
Bombasti
2楼-- · 2020-06-03 05:27

I found a solution for Linux. Basically you need ffmpeg library or libav-tools

Open terminal and type as root

apt-get install ffmpeg

or

apt-get install libav-tools

Hope it might help.

查看更多
登录 后发表回答