3D表面图中的自定义山体的阴影

演示在3D曲面图中使用自定义山体阴影。

3D表面图中的自定义山体的阴影示例

  1. # This import registers the 3D projection, but is otherwise unused.
  2. from mpl_toolkits.mplot3d import Axes3D # noqa: F401 unused import
  3. from matplotlib import cbook
  4. from matplotlib import cm
  5. from matplotlib.colors import LightSource
  6. import matplotlib.pyplot as plt
  7. import numpy as np
  8. # Load and format data
  9. filename = cbook.get_sample_data('jacksboro_fault_dem.npz', asfileobj=False)
  10. with np.load(filename) as dem:
  11. z = dem['elevation']
  12. nrows, ncols = z.shape
  13. x = np.linspace(dem['xmin'], dem['xmax'], ncols)
  14. y = np.linspace(dem['ymin'], dem['ymax'], nrows)
  15. x, y = np.meshgrid(x, y)
  16. region = np.s_[5:50, 5:50]
  17. x, y, z = x[region], y[region], z[region]
  18. # Set up plot
  19. fig, ax = plt.subplots(subplot_kw=dict(projection='3d'))
  20. ls = LightSource(270, 45)
  21. # To use a custom hillshading mode, override the built-in shading and pass
  22. # in the rgb colors of the shaded surface calculated from "shade".
  23. rgb = ls.shade(z, cmap=cm.gist_earth, vert_exag=0.1, blend_mode='soft')
  24. surf = ax.plot_surface(x, y, z, rstride=1, cstride=1, facecolors=rgb,
  25. linewidth=0, antialiased=False, shade=False)
  26. plt.show()

下载这个示例