Animations in Python with Matplotlib




[ad_1]

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/c09e8dfd54651cd94e75be15a2038e0e
https://gist.github.com/danielmk/f7d2c5f1be1ca9b9b4d1654509c3f990

Here is an animation I made with Matplotlib:
https://youtu.be/Jngqcak86NU

FuncAnimation docs:
https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.animation.FuncAnimation.html

Source


[ad_2]

Comment List

  • DMK Data
    December 27, 2020

    Thanks for the video, I have a question .. how did you download ffmpeg ? I can't find how to add it to my Anaconda version …

  • DMK Data
    December 27, 2020

    sir tell me how to save file i tried but i could not do

  • DMK Data
    December 27, 2020

    Why use dark theme. Nothing is visible.

  • DMK Data
    December 27, 2020

    Thank yo so much for the example.

Write a comment