Olly Smith Olly Smith - 1 year ago 135
Python Question

Saving a matplotlib animation in Linux

I'm having a problem saving a matplotlib animation. When I execute the following test script:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig = plt.figure()
ax = fig.add_subplot(111)

u, v, ims = [], [], []
for i in range(100):
ims.append(ax.plot(u, v, 'b-', linewidth=3.))

im_ani = animation.ArtistAnimation(fig, ims, interval=50, repeat_delay=3000,


I get the following error:

File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 712, in save
with writer.saving(self._fig, filename, dpi):
AttributeError: 'str' object has no attribute 'saving'

Now according to this answer, I need to install either ffmpeg or libav-tools. I tried this and found ffmpeg was not available, however libav-tools did seem to install properly. However, when I executed my script again, I still got the same error as before.

I also (following the advice of this answer) tried doing

mywriter = animation.FFMpegWriter()

but that didn't work either! It resulted in the following error:

File "anitest.py", line 22, in <module>
File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 712, in save
with writer.saving(self._fig, filename, dpi):
File "/usr/lib/python2.7/contextlib.py", line 17, in __enter__
return self.gen.next()
File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 169, in saving
File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 159, in setup
File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 186, in _run
File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1327, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory

Any help here would be much appreciated. I'm using Ubuntu 14.04. Thanks!

Answer Source

We arrived at a solution in the comments above. To summarise:

  • The reason for that rather cryptic error message:

    AttributeError: 'str' object has no attribute 'saving'

    is this bug in matplotlib which was fixed in version 1.4.0 (also mentioned here).

  • However, updating matplotlib to 1.4.0 or newer will not address the root cause of the problem, which is simply that ffmpeg is not installed (see here).

  • OP was having difficulty installing ffmpeg because it was dropped from the official Ubuntu repositories in version 14.04 (it was reinstated in Ubuntu 15.04). One work-around for those still using older versions of Ubuntu is to add this unofficial PPA:

    $ sudo add-apt-repository ppa:mc3man/trusty-media
    $ sudo apt-get update
    $ sudo apt-get dist-upgrade   # recommended on first use
    $ sudo apt-get install ffmpeg
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download