三维曲面(颜色贴图)

演示绘制使用coolwarm颜色贴图着色的3D表面。 使用antialiased = False使表面变得不透明。

还演示了使用LinearLocator和z轴刻度标签的自定义格式。

三维曲面(颜色贴图)示例

  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 import cm
  5. from matplotlib.ticker import LinearLocator, FormatStrFormatter
  6. import numpy as np
  7. fig = plt.figure()
  8. ax = fig.gca(projection='3d')
  9. # Make data.
  10. X = np.arange(-5, 5, 0.25)
  11. Y = np.arange(-5, 5, 0.25)
  12. X, Y = np.meshgrid(X, Y)
  13. R = np.sqrt(X**2 + Y**2)
  14. Z = np.sin(R)
  15. # Plot the surface.
  16. surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
  17. linewidth=0, antialiased=False)
  18. # Customize the z axis.
  19. ax.set_zlim(-1.01, 1.01)
  20. ax.zaxis.set_major_locator(LinearLocator(10))
  21. ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
  22. # Add a color bar which maps values to colors.
  23. fig.colorbar(surf, shrink=0.5, aspect=5)
  24. plt.show()

下载这个示例