使用预先计算的图像列表的动画图像

使用预先计算的图像列表的动画图像示例

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import matplotlib.animation as animation
  4. fig = plt.figure()
  5. def f(x, y):
  6. return np.sin(x) + np.cos(y)
  7. x = np.linspace(0, 2 * np.pi, 120)
  8. y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1)
  9. # ims is a list of lists, each row is a list of artists to draw in the
  10. # current frame; here we are just animating one artist, the image, in
  11. # each frame
  12. ims = []
  13. for i in range(60):
  14. x += np.pi / 15.
  15. y += np.pi / 20.
  16. im = plt.imshow(f(x, y), animated=True)
  17. ims.append([im])
  18. ani = animation.ArtistAnimation(fig, ims, interval=50, blit=True,
  19. repeat_delay=1000)
  20. # To save the animation, use e.g.
  21. #
  22. # ani.save("movie.mp4")
  23. #
  24. # or
  25. #
  26. # from matplotlib.animation import FFMpegWriter
  27. # writer = FFMpegWriter(fps=15, metadata=dict(artist='Me'), bitrate=1800)
  28. # ani.save("movie.mp4", writer=writer)
  29. plt.show()

下载这个示例