| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- # View more python tutorials on my Youtube and Youku channel!!!
- # Youtube video tutorial: https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg
- # Youku video tutorial: http://i.youku.com/pythontutorial
- # 19 - animation
- """
- Please note, this script is for python3+.
- If you are using python2+, please modify it accordingly.
- Tutorial reference:
- http://matplotlib.org/examples/animation/simple_anim.html
- More animation example code:
- http://matplotlib.org/examples/animation/
- """
- import numpy as np
- from matplotlib import pyplot as plt
- from matplotlib import animation
- fig, ax = plt.subplots()
- x = np.arange(0, 2*np.pi, 0.01)
- line, = ax.plot(x, np.sin(x))
- def animate(i):
- line.set_ydata(np.sin(x + i/10.0)) # update the data
- return line,
- # Init only required for blitting to give a clean slate.
- def init():
- line.set_ydata(np.sin(x))
- return line,
- # call the animator. blit=True means only re-draw the parts that have changed.
- # blit=True dose not work on Mac, set blit=False
- # interval= update frequency
- ani = animation.FuncAnimation(fig=fig, func=animate, frames=100, init_func=init,
- interval=20, blit=False)
- # 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
- # http://matplotlib.sourceforge.net/api/animation_api.html
- # anim.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])
- plt.show()
|