旋转3D线框图

一个非常简单的3D动画“动画”。 另请参见rotate_axes3d_demo。

(构建文档库时会跳过此示例,因为它有意运行需要很长时间)

  1. # This import registers the 3D projection, but is otherwise unused.
  2. from mpl_toolkits.mplot3d import Axes3D # noqa: F401 unused import
  3. import matplotlib.pyplot as plt
  4. import numpy as np
  5. import time
  6. def generate(X, Y, phi):
  7. '''
  8. Generates Z data for the points in the X, Y meshgrid and parameter phi.
  9. '''
  10. R = 1 - np.sqrt(X**2 + Y**2)
  11. return np.cos(2 * np.pi * X + phi) * R
  12. fig = plt.figure()
  13. ax = fig.add_subplot(111, projection='3d')
  14. # Make the X, Y meshgrid.
  15. xs = np.linspace(-1, 1, 50)
  16. ys = np.linspace(-1, 1, 50)
  17. X, Y = np.meshgrid(xs, ys)
  18. # Set the z axis limits so they aren't recalculated each frame.
  19. ax.set_zlim(-1, 1)
  20. # Begin plotting.
  21. wframe = None
  22. tstart = time.time()
  23. for phi in np.linspace(0, 180. / np.pi, 100):
  24. # If a line collection is already remove it before drawing.
  25. if wframe:
  26. ax.collections.remove(wframe)
  27. # Plot the new wireframe and pause briefly before continuing.
  28. Z = generate(X, Y, phi)
  29. wframe = ax.plot_wireframe(X, Y, Z, rstride=2, cstride=2)
  30. plt.pause(.001)
  31. print('Average FPS: %f' % (100 / (time.time() - tstart)))

下载这个示例