How Python uses matplotlib to draw animations

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

Matplotlib starting from 1.1.0 to support drawing animations

Here are a few examples:

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

#-*-Coding:utf-8-*-  import numpy as NP import Matplotlib.pyplot as PLT import matplotlib.animation as animation fi g = plt.figure () axes1 = Fig.add_subplot (111) 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, # 10 random Data def data_gen () generated each time:   While True:     yield Np.random.rand (ten) ANI = animation. Funcanimation (Fig, update, Data_gen, interval=2*1000) plt.show ()

The second example uses list (metric), each time a row of data from the metric is fed into the update as a parameter:

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  .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.figure () window = Fig.add_subplot (111) Line, = Window.plot (start) #如果是参数是list, the default is to fetch one element in the list each time, #即metric [0], METRIC[1],... def update (data):   line.set_ydata (data)   

A third example:

Import NumPy as NP from matplotlib import Pyplot as plt from matplotlib import Animation # First set up the figure, the ax is, and the plot element we want to animate fig = plt.figure () ax = plt.axes (xlim= (0, 2), ylim= ( -2, 2)) line, = Ax.plot ([] , [], lw=2) # initialization Function:plot The background of each frame def init ():   line.set_data ([], [])   return Line, # animation function. This was called sequentially # note:i is Framenumber def animate (i):   x = Np.linspace (0, 2, +)   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 has changed. Anim = animation. Funcanimation (Fig, animate, Init_func=init,                 

A fourth example:

#-*-Coding:utf-8-*-import numpy as NP import Matplotlib.pyplot as PLT import matplotlib.animation as animation # each generation A new coordinate point def data_gen ():   t = data_gen.t   cnt = 0   while cnt <:     cnt+=1     T + = 0.05     yield T, NP.S In (2*np.pi*t) * NP.EXP (-T/10.) data_gen.t = 0 # drawing Fig, ax = plt.subplots () line, = Ax.plot ([], [], lw=2) Ax.set_ylim (-1.1 , 1.1) Ax.set_xlim (0, 5) ax.grid () xdata, Ydata = [], [] # because the parameter of run is called function data_gen,# so the first argument can be not framenum: Set line data, return to Line de F Run (data):   # Update the data   t,y = data   xdata.append (t)   ydata.append (y)   xmin, xmax = Ax.get_xlim ()   If T >= xmax:     ax.set_xlim (xmin, 2*xmax)     Ax.figure.canvas.draw ()   line.set_data (XData, Ydata)   return line, # calls the function Run,run every 10 seconds for the function Data_gen, # indicates that the graph only updates the element that needs to be drawn, Ani = animation. Funcanimation (Fig, run, Data_gen, Blit=true, interval=10,   

Let's look at the following example:

#-*-Coding:utf-8-*-import numpy as NP import Matplotlib.pyplot as PLT import matplotlib.animation as animation #第一个参数 Must be 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 increased from 1 to 25, returning again from 1 to 25, then returning ... 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 to know Infinity #由于frame达到25以后, the data no longer change, so you will find that after reaching 25, the graph no longer changes #line_ani = animation. Funcanimation (FIG1, Update_line, fargs= (data, L), #  

Hopefully this article will help you with Python programming.

  • Related Article

    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.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.