Python - animation with matplotlib.pyplot


Question

How can one create animated diagrams using popular matplotlib library? I am particularly interested in animated gifs.

1
22
3/30/2010 4:16:12 PM

Accepted Answer

The matplotlib docs provide an entire section of examples on animation (see this scipy tutorial also). Most, however, involve using the various GUI widget backends. There is one in there, "movie demo", that shows how to produce an avi of a series of PNGS.

To produce animated GIFs, I think your options are pretty limited. Last I checked, PIL didn't support them. You could however generate a series of PNGs using pyplot's savefig and then stitch them together using a call to ImageMagick or mencoder.

23
7/30/2018 8:56:10 AM

This question is kind of old but nevertheless I will answer just in case someone crash here and this can be useful.

So nowadays it seems that the best way to go in matplotlib is to use the functionality that they provide:

http://matplotlib.org/api/animation_api.html#module-matplotlib.animation

They also include some useful examples which you can easily modify for your needs (Though the explanation on how they work will be hard to grasp if you haven't used matplotlib in a class oriented way):

http://matplotlib.org/examples/animation/

Finally just to complement this information Jake VanderPlas has a more complete tutorial and a brief review on the future of visualization in Python:

Tutorial: http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/

Essay about visualization: http://jakevdp.github.io/blog/2013/03/23/matplotlib-and-the-future-of-visualization-in-python/


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon