title: matplotlib.animation: how to plot an animation and save it in a file simultaneously?
body: <p>With <code>animation</code> from <code>matplotlib</code> we can plot dynamic animations. Installing the <code>ffmpeg</code> codec we can save these animations into files.</p>
  
  <p>But how can I make both showing the animation while an algorithm is running and saving this animation into a file? </p>
  
  <p>This code <a href="http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/" rel="nofollow">(full snippet in this tutorial)</a> does not work for me. It saves the file but it does not show the animation on the fly.</p>
  
  <pre><code>ani = animation.FuncAnimation(fig, animate, frames=300,
                            interval=interval, blit=True, init_func=init)
  
  # save the animation as an mp4.  This requires ffmpeg or mencoder to be
  # installed.  The extra_args ensure that the x264 codec is used, so that    
  # the video can be embedded in html5.  You may need to adjust this for
  # your system: for more information, see
  # http://matplotlib.sourceforge.net/api/animation_api.html
  ani.save('double_pendulum.mp4', fps=30, extra_args=['-vcodec', 'libx264'])
  plt.show()
  </code></pre>