1+ # View more python tutorials on my Youtube and Youku channel!!!
2+
3+ # Youtube video tutorial: https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg
4+ # Youku video tutorial: http://i.youku.com/pythontutorial
5+
6+ # 19 - animation
7+ """
8+ Please note, this script is for python3+.
9+ If you are using python2+, please modify it accordingly.
10+
11+ Tutorial reference:
12+ http://matplotlib.org/examples/animation/simple_anim.html
13+
14+ More animation example code:
15+ http://matplotlib.org/examples/animation/
16+ """
17+
18+ import numpy as np
19+ from matplotlib import pyplot as plt
20+ from matplotlib import animation
21+
22+ fig , ax = plt .subplots ()
23+
24+ x = np .arange (0 , 2 * np .pi , 0.01 )
25+ line , = ax .plot (x , np .sin (x ))
26+
27+
28+ def animate (i ):
29+ line .set_ydata (np .sin (x + i / 10.0 )) # update the data
30+ return line ,
31+
32+
33+ # Init only required for blitting to give a clean slate.
34+ def init ():
35+ line .set_ydata (np .ma .array (x , mask = True ))
36+ return line ,
37+
38+ # call the animator. blit=True means only re-draw the parts that have changed.
39+ # blit=True dose not work on Mac, set blit=False
40+ # interval= update frequency
41+ ani = animation .FuncAnimation (fig = fig , func = animate , frames = 100 , init_func = init ,
42+ interval = 20 , blit = False )
43+
44+ # save the animation as an mp4. This requires ffmpeg or mencoder to be
45+ # installed. The extra_args ensure that the x264 codec is used, so that
46+ # the video can be embedded in html5. You may need to adjust this for
47+ # your system: for more information, see
48+ # http://matplotlib.sourceforge.net/api/animation_api.html
49+ # anim.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])
50+
51+ plt .show ()
0 commit comments