Animations in Python with Matplotlib

Published: 30 November 2019
on channel: DMK Data
21,249
174

You can find my blog post about animations with Matplotlib here: http://danielmuellerkomorowska.com/20...

Animations with Matplotlib are easy and anything that can be plotted in a figure can also be animated. We only need the FuncAnimation class which we can import from matplotlib.animation. The only required parameters to create an instance of FuncAnimation are fig, the figure where the animation is supposed to happend, and func, the function that generates each frame in sequence. We can write func ourselves and for most purposes it requires only one parameter, called i. Since an animation requires some sort of change between each frame we need to change what exactly func does and i allows us to do exactly that. An optional parameter of FuncAnimation called frames allows us to specify what exactly is passed to i for each frame. By default it is just a sequence (an itertools.count call to be precise).
In this particular example, frames is a sequence with a step size of 50 and the parameter i tells func which parts of the data to plot for a particular frame. There is one more function called init_func, which is optional but it is important, especially for repeating animations. init_func is called at the beginning of the animation and whenever the animation repeats. So it has to reset the initial state of the figure.
That's it. Let me know if you have questions about animations with Matplotlib.

Example code from this video:
https://gist.github.com/danielmk/c09e...
https://gist.github.com/danielmk/f7d2...

Here is an animation I made with Matplotlib:
   • TSNE of MNIST Test Dataset  

FuncAnimation docs:
https://matplotlib.org/3.1.1/api/_as_...


Watch video Animations in Python with Matplotlib online without registration, duration hours minute second in high quality. This video was added by user DMK Data 30 November 2019, don't forget to share it with your friends and acquaintances, it has been viewed on our site 21,249 once and liked it 174 people.