Animations in Python with Matplotlib
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.
Here is an animation I made with Matplotlib: