How Python uses matplotlib to draw animations

Source: Internet
Author: User
Tags rand sin

This example describes how Python uses matplotlib to draw animations. Share to everyone for your reference. The specific analysis is as follows:

Matplotlib supports drawing animations since version 1.1.0

Here are a few examples:

The first example uses generator, which runs function Data_gen every two seconds:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17-18 #-*-Coding:utf-8-*-import numpy as NP import Matplotlib.pyplot as PLT import matplotlib.animation as animation fig = Plt.figure () axes1 = Fig.add_subplot line, = Axes1.plot (Np.random.rand) #因为update的参数是调用函数data_gen, # So the first default parameter cannot be framenum def update (data): Line.set_ydata (data) return line, # generate 10 random Data def Data_gen (): While True:yield n P.random.rand (a) ANI = animation. Funcanimation (Fig, update, Data_gen, interval=2*1000) plt.show ()

The second example uses list (metric) to send a row of data from the metric to the update as a parameter:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17-18 Import NumPy as NP import Matplotlib.pyplot as PLT import matplotlib.animation as animation start = [1, 0.18, 0.63, 0.29, 0.03, 0.24, 0.86, 0.07, 0.58, 0] metric =[[0.03, 0.86, 0.65, 0.34, 0.34, 0.02, 0.22, 0.74, 0.66, 0.65], [0.43, 0.18, 0.63, 0.29, 0.03, 0.24, 0.86, 0.07, 0.58, 0.55], [0.66, 0.75, 0.01, 0.94, 0.72, 0.77, 0.20, 0.66, 0.81, 0.52]] Fig = Plt.figur E () window = Fig.add_subplot line, = Window.plot (start) #如果是参数是list, the default is to fetch one element of the list at a time, #即metric [0],metric[1],... def update (data): Line.set_ydata (data) return line, ANI = animation. Funcanimation (Fig, UPDATE, metric, interval=2*1000) plt.show ()

A third example:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 import NumPy as NP from matplotlib import Pyplot as plt from Matplotlib im Port Animation # The figure, the axis, and the plot element we want to animate fig = plt.figure () ax = plt.ax ES (xlim= (0, 2), ylim= ( -2, 2)) line, = Ax.plot ([], [], lw=2) # initialization Function:plot The background of each frame D EF init (): Line.set_data ([], []) return line, # animation function. This is called sequentially # note:i is Framenumber def animate (i): x = Np.linspace (0, 2, 1000) y = Np.sin (2 * np.pi * (x -0.01 * i) line.set_data (x, y) return line, # call the animator. Blit=true means only re-draw the parts that have changed. Anim = animation. Funcanimation (Fig, animate, Init_func=init, frames=200, interval=20, Blit=true) #anim. Save (' Basic_animation.mp4 ', fps =30, extra_args=['-vcodec ', ' libx264 '] plt.show ()

A fourth example:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 This is the

Look at the following example:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #-*-coding:utf-8-*-import numpy as NP import Matplotlib.pyplot As PLT import matplotlib.animation as animation #第一个参数必须为framenum def update_line (num, data, line): Line.set_data (data[. .,: num]) return line, FIG1 = plt.figure () data = Np.random.rand (2) L, = Plt.plot ([], [], ' R ') Plt.xlim (0, 1) plt.ylim (0, 1) plt.xlabel (' x ') plt.title (' Test ') #framenum从1增加大25后, return from 1 to 25 again, then return ... Line_ani = animation. Funcanimation (FIG1, Update_line, 25,fargs= (data, L), interval=50, blit=true) #等同于 #line_ani = animation. Funcanimation (FIG1, Update_line, frames=25,fargs= (data, L), # interval=50, blit=true) #忽略frames参数, Framenum will increase from 11 straight to know the infinite #由于frame达到25以后, the data no longer change, so you will find that when you reach 25, the graph no longer changes #line_ani = animation. Funcanimation (FIG1, Update_line, fargs= (data, L), # interval=50, Blit=true) plt.show ()

I hope this article will help you with your Python programming.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.