20. Basic animation

This script provides a minimal, fundamental example of how to create an animation in Matplotlib using the FuncAnimation class. It initializes an empty line plot and defines an update function that progressively adds data points to the line for each frame. FuncAnimation then repeatedly calls this function to generate the final animation, creating the effect of a line being drawn across the plot.

Out:

C:\Users\kelda\Desktop\repositories\github\python-spare-code\main\examples\matplotlib\plot_main20_animation.py:33: UserWarning:

FigureCanvasAgg is non-interactive, and thus cannot be shown

14 import numpy as np
15 import matplotlib.pyplot as plt
16 import matplotlib.animation as animation
17
18 # Adapted from
19 # https://matplotlib.org/gallery/animation/basic_example.html
20
21
22 def _update_line(num):
23     line.set_data(data[..., :num])
24     return line,
25
26
27 fig, ax = plt.subplots()
28 data = np.random.RandomState(0).rand(2, 25)
29 line, = ax.plot([], [], 'r-')
30 ax.set(xlim=(0, 1), ylim=(0, 1))
31 ani = animation.FuncAnimation(fig, _update_line, 25, interval=100, blit=True)
32
33 plt.show()

Total running time of the script: ( 0 minutes 2.100 seconds)

Gallery generated by Sphinx-Gallery