着色示例

显示如何制作阴影浮雕图的示例,如Mathematica (http://reference.wolfram.com/mathematica/ref/ReliefPlot.html)或通用映射工具(https://gmt.soest.hawaii.edu/)

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from matplotlib.colors import LightSource
  4. from matplotlib.cbook import get_sample_data
  5. def main():
  6. # Test data
  7. x, y = np.mgrid[-5:5:0.05, -5:5:0.05]
  8. z = 5 * (np.sqrt(x**2 + y**2) + np.sin(x**2 + y**2))
  9. filename = get_sample_data('jacksboro_fault_dem.npz', asfileobj=False)
  10. with np.load(filename) as dem:
  11. elev = dem['elevation']
  12. fig = compare(z, plt.cm.copper)
  13. fig.suptitle('HSV Blending Looks Best with Smooth Surfaces', y=0.95)
  14. fig = compare(elev, plt.cm.gist_earth, ve=0.05)
  15. fig.suptitle('Overlay Blending Looks Best with Rough Surfaces', y=0.95)
  16. plt.show()
  17. def compare(z, cmap, ve=1):
  18. # Create subplots and hide ticks
  19. fig, axs = plt.subplots(ncols=2, nrows=2)
  20. for ax in axs.flat:
  21. ax.set(xticks=[], yticks=[])
  22. # Illuminate the scene from the northwest
  23. ls = LightSource(azdeg=315, altdeg=45)
  24. axs[0, 0].imshow(z, cmap=cmap)
  25. axs[0, 0].set(xlabel='Colormapped Data')
  26. axs[0, 1].imshow(ls.hillshade(z, vert_exag=ve), cmap='gray')
  27. axs[0, 1].set(xlabel='Illumination Intensity')
  28. rgb = ls.shade(z, cmap=cmap, vert_exag=ve, blend_mode='hsv')
  29. axs[1, 0].imshow(rgb)
  30. axs[1, 0].set(xlabel='Blend Mode: "hsv" (default)')
  31. rgb = ls.shade(z, cmap=cmap, vert_exag=ve, blend_mode='overlay')
  32. axs[1, 1].imshow(rgb)
  33. axs[1, 1].set(xlabel='Blend Mode: "overlay"')
  34. return fig
  35. if __name__ == '__main__':
  36. main()

着色示例

着色示例2

参考

本例中显示了以下函数、方法和类的使用:

  1. import matplotlib
  2. matplotlib.colors.LightSource
  3. matplotlib.axes.Axes.imshow
  4. matplotlib.pyplot.imshow

下载这个示例