Justin Furuness
Justin Furuness

Reputation: 986

Matplotlib can not save animation

I have a matplotlib animation and it will not save. If I do not save it, it runs totally fine and without error. When I try to save it errors with a message that is not helpful. I have googled this error and checked everything, but I cannot seem to find an answer to this problem. I have installed ffmpeg. Am I doing something wrong that is obvious? I am running on ubuntu 19.10 with matplotlib 3.2.1 if that matters.

The code to save the animation is below:

    def run_animation(self, total_rounds):
        anim = animation.FuncAnimation(self.fig, self.animate,
                                       init_func=self.init,
                                       frames=total_rounds * 100,
                                       interval=40,
                                       blit=True)
#        Writer = animation.writers['ffmpeg']
#        writer = Writer(fps=15, metadata=dict(artist='Me'), bitrate=1800)
        anim.save('animation.mp4')

The error traceback:

2020-04-01 02:20:58,279-INFO: MovieWriter._run: running command: ffmpeg -f rawvideo -vcodec rawvideo -s 1200x500 -pix_fmt rgba -r 25.0 -loglevel error -i pipe: -vcodec h264 -pix_fmt yuv420p -y animation.mp4
Traceback (most recent call last):
  File "/home/anon/.local/lib/python3.7/site-packages/matplotlib/backend_bases.py", line 2785, in _wait_cursor_for_draw_cm
    self.set_cursor(cursors.WAIT)
  File "/home/anon/.local/lib/python3.7/site-packages/matplotlib/backends/backend_gtk3.py", line 468, in set_cursor
    self.canvas.get_property("window").set_cursor(cursord[cursor])
AttributeError: 'NoneType' object has no attribute 'set_cursor'

Thanks a million for your help

Upvotes: 1

Views: 2364

Answers (1)

Justin Furuness
Justin Furuness

Reputation: 986

I figured it out, oddly enough I needed to do this before all my import statements.

import matplotlib
matplotlib.use("Agg")

If I did not have that, it wouldn't work. Also, ffmpeg started taking a while, so I modified the save function to be like this:

anim.save('animation.mp4', progress_callback=lambda i, n: print(f'Saving frame {i} of {n}'))

Kind of a nice hidden feature in the docs. Hope no one else has this issue!

Upvotes: 5

Related Questions