动画线图

动画线图

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import matplotlib.animation as animation
  4. fig, ax = plt.subplots()
  5. x = np.arange(0, 2*np.pi, 0.01)
  6. line, = ax.plot(x, np.sin(x))
  7. def init(): # only required for blitting to give a clean slate.
  8. line.set_ydata([np.nan] * len(x))
  9. return line,
  10. def animate(i):
  11. line.set_ydata(np.sin(x + i / 100)) # update the data.
  12. return line,
  13. ani = animation.FuncAnimation(
  14. fig, animate, init_func=init, interval=2, blit=True, save_count=50)
  15. # To save the animation, use e.g.
  16. #
  17. # ani.save("movie.mp4")
  18. #
  19. # or
  20. #
  21. # from matplotlib.animation import FFMpegWriter
  22. # writer = FFMpegWriter(fps=15, metadata=dict(artist='Me'), bitrate=1800)
  23. # ani.save("movie.mp4", writer=writer)
  24. plt.show()

下载这个示例