3

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)
ax.set_ylim([0,11])
ax.set_xlim([0,100])

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

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

im_ani.save('c.mp4')   

I get the following error:

im_ani.save('c.mp4')   
  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()
anim.save('mymovie.mp4',writer=mywriter)

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

  File "anitest.py", line 22, in <module>
    im_ani.save('mymovie.mp4',writer=mywriter)    
  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
    self.setup(*args)
  File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 159, in setup
    self._run()
  File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 186, in _run
    stdin=subprocess.PIPE)
  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!

Community
  • 1
  • 1
Olly Smith
  • 33
  • 1
  • 3
  • The first error you are seeing is caused by [this bug](https://github.com/matplotlib/matplotlib/issues/2651) in matplotlib. It was fixed in v1.4.0, so you should probably consider updating. – ali_m Dec 20 '15 at 19:07
  • Not sure what you mean by *"ffmpeg was not available"* - did you try `sudo apt-get install ffmpeg`? – ali_m Dec 20 '15 at 19:11
  • Hi, yes I did `sudo apt-get install ffmpeg` and got `Package ffmpeg is not available, but is referred to by another package. This may mean that the package is missing, has been obsoleted, or is only available from another source E: Package 'ffmpeg' has no installation candidate ` – Olly Smith Dec 20 '15 at 19:19
  • How do I go about updating matplotlib? I tried `sudo apt-get update` followed by `sudo apt-get install python-matplotlib` and it said that matplotlib is already the latest version. – Olly Smith Dec 20 '15 at 19:23
  • Updating matplotlib won't solve this particular problem - it will just mean that you get a more informative error message. It seems that `ffmpeg` was [removed from the official repositories in 14.04](http://askubuntu.com/a/432585/185188), but you can install it by adding [this unofficial PPA](https://launchpad.net/~mc3man/+archive/ubuntu/trusty-media). – ali_m Dec 20 '15 at 19:29
  • If you decide that you do want to update matplotlib, I suggest you set up a separate [virtualenv](https://virtualenv.readthedocs.org/en/latest/) so that you don't have to mess with the system-wide version in `/usr/lib`. Installing the backend dependencies inside a virtualenv can be a bit fiddly - see [here](http://matplotlib.org/faq/virtualenv_faq.html?highlight=virtualenv) for more info. – ali_m Dec 20 '15 at 19:32
  • Christ this is more complicated than I thought it would be. I can't believe it's this awkward to fix - I'm using the most widely used python plotting library on the most widely used linux distro. It's not like I'm wanting to do something obscure. I'll give this stuff a read – Olly Smith Dec 20 '15 at 20:14
  • You can partly blame the Ubuntu maintainers for dropping `ffmpeg` from the official repos :-) As I said, updating matplotlib is purely optional - the simplest solution would probably be just to add that PPA and install `ffmpeg` from it. – ali_m Dec 20 '15 at 20:47
  • Thanks! I added the ppa and installed ffmpeg from it :) Everything works fine now. Thanks for the help! – Olly Smith Dec 20 '15 at 21:04

2 Answers2

3

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
    
Akababa
  • 327
  • 3
  • 21
ali_m
  • 71,714
  • 23
  • 223
  • 298
0

1) Make sure you using matplotlib V1.4 or higher

python -c 'import matplotlib;print matplotlib.__version__'

2a) Try to install ffmpeg, on 14.04 probably you will fail. if so, go to 2b)

2b) then install libav-tools:

 sudo apt-get install libav-tools

and use this clause to save animation

anim.save('image.mp4', fps=20, writer="avconv", codec="libx264")
Bourbia Brahim
  • 14,459
  • 4
  • 39
  • 52