Frontpage 3D示例

此示例再现Frontpage 3D示例。

Frontpage 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. filename = cbook.get_sample_data('jacksboro_fault_dem.npz', asfileobj=False)
  9. with np.load(filename) as dem:
  10. z = dem['elevation']
  11. nrows, ncols = z.shape
  12. x = np.linspace(dem['xmin'], dem['xmax'], ncols)
  13. y = np.linspace(dem['ymin'], dem['ymax'], nrows)
  14. x, y = np.meshgrid(x, y)
  15. region = np.s_[5:50, 5:50]
  16. x, y, z = x[region], y[region], z[region]
  17. fig, ax = plt.subplots(subplot_kw=dict(projection='3d'))
  18. ls = LightSource(270, 45)
  19. # To use a custom hillshading mode, override the built-in shading and pass
  20. # in the rgb colors of the shaded surface calculated from "shade".
  21. rgb = ls.shade(z, cmap=cm.gist_earth, vert_exag=0.1, blend_mode='soft')
  22. surf = ax.plot_surface(x, y, z, rstride=1, cstride=1, facecolors=rgb,
  23. linewidth=0, antialiased=False, shade=False)
  24. ax.set_xticks([])
  25. ax.set_yticks([])
  26. ax.set_zticks([])
  27. fig.savefig("surface3d_frontpage.png", dpi=25) # results in 160x120 px image

下载这个示例