SoFunction
Updated on 2024-11-17

Python draw animation through matplotlib simple example

Matplotlib is a 2D plotting library for Python that generates publication-quality level graphics in a variety of hardcopy formats and cross-platform interactive environments.

With Matplotlib, developers can generate plots, histograms, power spectra, bar charts, error plots, scatter plots, and more with just a few lines of code.

Matplotlib has supported drawing animations since version 1.1.0, for more information on how to use it seeOfficial Help Documentation. Here is a very basic example:

"""
A simple example of an animated plot
"""
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
# First set up the figure, the axis, and the plot element we want to animate
fig = ()
# create our line object which will be modified in the animation
ax = (xlim=(0, 2), ylim=(-2, 2))
# we simply plot an empty line: we'll add data to the line later
line, = ([], [], lw=2) 
# initialization function: plot the background of each frame
def init():
 line.set_data([], [])
 return line,
# animation function. This is called sequentially
# It takes a single parameter, the frame number i 
def animate(i):
 x = (0, 2, 1000)
 y = (2 *  * (x - 0.01 * i)) # update the data
 line.set_data(x, y)
 return line,
# Makes an animation by repeatedly calling a function func
# frames can be a generator, an iterable, or a number of frames.
# interval draws a new frame every interval milliseconds.
# blit=True means only re-draw the parts that have changed.
# Set up a 200-frame animation here, with 20 milliseconds between each frame #
anim = (fig, animate, init_func=init,
        frames=200, interval=20, blit=True)
# save the animation as an mp4. This requires ffmpeg or mencoder to be
# installed. The extra_args ensure that the x264 codec is used, so that
# the video can be embedded in html5. You may need to adjust this for
# your system: for more information, see
# /api/animation_api.html
('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])

() # () The animation will keep looping.

Results:

If you want to save the animation as a video file in mp4 format, you need to first install theFFmpeg. FFmpeg is a set of open source computer programs that can be used to record and convert digital audio and video into streams. Under the LGPL or GPL license. It provides a complete solution for recording, converting, and streaming audio and video.

Download the windows version here:DownloadFFmpegforWindowsIf you want to use ffmpeg-3.2.2, unzip ffmpeg-3.2.2 and add the bin directory to the path of the system environment variable. For example: C:\ProgramFiles\ffmpeg-3.2.2-win64-static\bin. Then test if the configuration is OK: enter ffmpeg-version

summarize

Above is this article on Python through matplotlib draw animation simple example of the whole content, I hope you can help. Interested friends can continue to refer to other related topics on this site, if there are inadequacies, welcome to leave a message to point out. Thank you for the support of friends on this site!