Python Drawing Animation Sample __python

Source: Internet
Author: User
Tags hypot rand sin

Suit, first look at the Python matpotlib official website http://matplotlib.org/examples/index.html on the completion of what the example, after all, their own research API too time-consuming

1,


Import NumPy as NP
import Matplotlib.pyplot as Plt
import matplotlib.animation as animation


def Data_gen (t=0 ):
    cnt = 0
    while CNT < 1000:
        cnt = 1
        t + + 0.1
        yield T, Np.sin (2*np.pi*t) * NP.EXP (-T/10.)


DEF init ():
    Ax.set_ylim ( -1.1, 1.1)
    Ax.set_xlim (0,)
    del xdata[:]
    del ydata[:]
    line.set_ Data (XData, Ydata) return line
    ,

fig, ax = plt.subplots () line
, = Ax.plot ([], [], lw=2)
Ax.grid () C18/>xdata, Ydata = [], []


def Run (data):
    # Update the data
    t, y = data
    xdata.append (t)
    Ydata.ap Pend (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

    ,

ani = animation. Funcanimation (Fig, run, Data_gen, Blit=false, interval=10,
                              repeat=false, Init_func=init)
plt.show ()

2,


Import NumPy as NP
import Matplotlib.pyplot as Plt
import matplotlib.animation as animation

fig = plt.figure (


def f (x, y): Return
    np.sin (x) + Np.cos (y)

x = Np.linspace (0, 2 * np.pi,)
y = np.linspace (0, 2 * np.p Reshape ( -1, 1)

im = Plt.imshow (f (x, y), animated=true)


def updatefig (*args):
    global x, y
    x + + NP . Pi/15.
    Y + + np.pi/20.
    Im.set_array (f (x, y)) return
    IM,

ani = animation. Funcanimation (Fig, Updatefig, interval=50, blit=true)
plt.show ()

3,



Import NumPy as NP import Matplotlib.pyplot as PLT import matplotlib.animation as animation def update_line (num, data, L INE): 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 ') Line_ani = animation. Funcanimation (FIG1, Update_line, fargs= (data, L), interval=50, Blit=true) # to Sav  E The animation, use the Command:line_ani.save (' lines.mp4 ') Fig2 = Plt.figure () x = Np.arange ( -9,) y = Np.arange (-9, Reshape ( -1, 1) base = Np.hypot (x, y) IMS = [] for add in Np.arange: Ims.append (Plt.pcolor (x, Y, base + Add, Norm=plt. Normalize (0)) Im_ani = animation. Artistanimation (FIG2, IMS, INTERVAL=50, repeat_delay=3000, Blit=true) # to save this SE Cond animation with some metadata, use the following command: # im_ani.save (' Im.mp4 ', metadata={' artist ': ' Guido '}) PLT.show () 

4,


Import NumPy as NP import Matplotlib.pyplot as PLT import matplotlib.patches as patches import Matplotlib.path as Path IM Port matplotlib.animation as animation fig, ax = plt.subplots () # Histogram our data with numpy data = NP.RANDOM.RANDN (1 N, bins = np.histogram (data, M) # Get the corners of the rectangles for the histogram left = Np.array (Bins[:-1]) r ight = Np.array (bins[1:]) bottom = Np.zeros (len) top = bottom + N nrects = Len (left) # This comes the tricky part- -We have to set up the vertex and path # codes arrays using MoveTo, LineTo and Closepoly To, 3 for the LINETO, 1 for the # closepoly; The vert for the Closepoly are ignored but we still need # it to keep the codes aligned with the vertices = nverts ( 1 + 3 + 1) verts = Np.zeros ((nverts, 2)) codes = Np.ones (nverts, int) * path. Path.lineto Codes[0::5] = path. Path.moveto Codes[4::5] = path. Path.closepoly verts[0::5, 0] = left verts[0::5, 1] = bottom Verts[1::5, 0] = left VErts[1::5, 1] = top Verts[2::5, 0] = right verts[2::5, 1] = top Verts[3::5, 0] = right verts[3::5, 1] = bottom Barpath = Path. Path (verts, codes) Patch = patches. Pathpatch (Barpath, facecolor= ' green ', edgecolor= ' yellow ', alpha=0.5) ax.add_patch (patch) Ax.set_xlim (left[0), right[ -1]) Ax.set_ylim (Bottom.min (), Top.max ()) def animate (i): # Simulate new data coming in data = NP.RANDOM.RANDN (1 N, bins = np.histogram (data,) top = bottom + N verts[1::5, 1] = top Verts[2::5, 1] = top Retu RN [Patch,] ani = animation. Funcanimation (Fig, animate, Repeat=false, Blit=true) plt.show ()

5, the animation to save video files, first installed sudo apt-get install FFmpeg

Import NumPy as NP import matplotlib matplotlib.use ("Agg") import Matplotlib.pyplot as PLT import matplotlib.animation as  Animation def update_line (num, data, line): Line.set_data (data[..., num]) return line, # Set up formatting for


The movie files Writer = animation.writers[' ffmpeg '] Writer = Writer (fps=15, metadata=dict (artist= ' Me '), bitrate=1800) 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 ') Line_ani = animation. Funcanimation (FIG1, Update_line, fargs= (data, L), interval=50, blit=true) ffwriter = Animation. Ffmpegwriter () line_ani.save (' Basic_animation.mp4 ', writer = Ffwriter, fps=30, extra_args=['-vcodec ', ' libx264 ']) # 
Line_ani.save (' Lines.mp4 ', writer=writer) Fig2 = Plt.figure () x = Np.arange ( -9,) y = Np.arange ( -9, ten). Reshape (-1, 1) Base = Np.hypot (x, y) IMS = [] for add in Np.arange: Ims.append (Plt.pcolor (x, y, base + adD, Norm=plt. Normalize (0)) Im_ani = animation. Artistanimation (FIG2, IMS, INTERVAL=50, repeat_delay=3000, Blit=true) #im_ani. Save (' im.
 MP4 ', Writer=writer)




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.