三维曲面(棋盘格)

演示绘制以棋盘图案着色的3D表面。

三维曲面(棋盘格)示例

  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. from matplotlib.ticker import LinearLocator
  5. import numpy as np
  6. fig = plt.figure()
  7. ax = fig.gca(projection='3d')
  8. # Make data.
  9. X = np.arange(-5, 5, 0.25)
  10. xlen = len(X)
  11. Y = np.arange(-5, 5, 0.25)
  12. ylen = len(Y)
  13. X, Y = np.meshgrid(X, Y)
  14. R = np.sqrt(X**2 + Y**2)
  15. Z = np.sin(R)
  16. # Create an empty array of strings with the same shape as the meshgrid, and
  17. # populate it with two colors in a checkerboard pattern.
  18. colortuple = ('y', 'b')
  19. colors = np.empty(X.shape, dtype=str)
  20. for y in range(ylen):
  21. for x in range(xlen):
  22. colors[x, y] = colortuple[(x + y) % len(colortuple)]
  23. # Plot the surface with face colors taken from the array we made.
  24. surf = ax.plot_surface(X, Y, Z, facecolors=colors, linewidth=0)
  25. # Customize the z axis.
  26. ax.set_zlim(-1, 1)
  27. ax.w_zaxis.set_major_locator(LinearLocator(6))
  28. plt.show()

下载这个示例