Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't save matplotlib animation

I am trying to get a simple animation saved using ffmpeg. I followed a tutorial to install ffmpeg, and I can now access it from the command prompt.

Now I run this piece of code:

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

fig = plt.figure()
ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))
line, = ax.plot([], [], lw=2)

def init():
    line.set_data([], [])
    return line,

def animate(i):
    x = np.linspace(0, 2, 1000)
    y = np.sin(2 * np.pi * (x - 0.01 * i))
    line.set_data(x, y)
    return line,

anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=200, interval=20, blit=True)

mywriter = animation.FFMpegWriter()
anim.save('mymovie.mp4',writer=mywriter)

plt.show()

I get this error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
   File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 523, in runfile
    execfile(filename, namespace)
  File "C:\Users\Renger\.xy\startups\b.py", line 23, in <module>
    anim.save('mymovie.mp4',writer=mywriter)
  File "C:\Python27\lib\site-packages\matplotlib\animation.py", line 609, in save
    with writer.saving(self._fig, filename, dpi):
  File "C:\Python27\lib\contextlib.py", line 17, in __enter__
    return self.gen.next()
  File "C:\Python27\lib\site-packages\matplotlib\animation.py", line 166, in saving
    self.setup(*args)
  File "C:\Python27\lib\site-packages\matplotlib\animation.py", line 156, in setup
    self._run()
  File "C:\Python27\lib\site-packages\matplotlib\animation.py", line 183, in _run
    stdin=subprocess.PIPE)
  File "C:\Python27\lib\subprocess.py", line 711, in __init__
    errread, errwrite)
  File "C:\Python27\lib\subprocess.py", line 948, in _execute_child
    startupinfo)
WindowsError: [Error 2] Het systeem kan het opgegeven bestand niet vinden

The last dutch sentence does mean something like: The system can't find the specified file.

What do these errors mean, and how can I solve them?

like image 391
renger Avatar asked May 25 '14 15:05

renger


People also ask

How do I save a Matplotlib animation?

To save an animation, we can use Animation. save() or Animation. to_html5_video(). Animation.

How do I save an animation as a mp4 in Python?

To save this animation as an mp4 you can simply call ani. save() . If you just want to take a look at it before you save it call plt. show() instead.


1 Answers

You need to specify your path to ffmpeg:

On linux I use:

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
plt.rcParams['animation.ffmpeg_path'] = '/usr/bin/ffmpeg'

You will obviously have to point to your windows .exe instead of '/usr/bin/ffmpeg'

If you don't have ffmpeg installed, you can get it here

like image 195
Padraic Cunningham Avatar answered Oct 04 '22 10:10

Padraic Cunningham