晕渲

用阴影图展示一些常见的技巧。

晕渲示例

晕渲示例2

晕渲示例3

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from matplotlib.colors import LightSource, Normalize
  4. def display_colorbar():
  5. """Display a correct numeric colorbar for a shaded plot."""
  6. y, x = np.mgrid[-4:2:200j, -4:2:200j]
  7. z = 10 * np.cos(x**2 + y**2)
  8. cmap = plt.cm.copper
  9. ls = LightSource(315, 45)
  10. rgb = ls.shade(z, cmap)
  11. fig, ax = plt.subplots()
  12. ax.imshow(rgb, interpolation='bilinear')
  13. # Use a proxy artist for the colorbar...
  14. im = ax.imshow(z, cmap=cmap)
  15. im.remove()
  16. fig.colorbar(im)
  17. ax.set_title('Using a colorbar with a shaded plot', size='x-large')
  18. def avoid_outliers():
  19. """Use a custom norm to control the displayed z-range of a shaded plot."""
  20. y, x = np.mgrid[-4:2:200j, -4:2:200j]
  21. z = 10 * np.cos(x**2 + y**2)
  22. # Add some outliers...
  23. z[100, 105] = 2000
  24. z[120, 110] = -9000
  25. ls = LightSource(315, 45)
  26. fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(8, 4.5))
  27. rgb = ls.shade(z, plt.cm.copper)
  28. ax1.imshow(rgb, interpolation='bilinear')
  29. ax1.set_title('Full range of data')
  30. rgb = ls.shade(z, plt.cm.copper, vmin=-10, vmax=10)
  31. ax2.imshow(rgb, interpolation='bilinear')
  32. ax2.set_title('Manually set range')
  33. fig.suptitle('Avoiding Outliers in Shaded Plots', size='x-large')
  34. def shade_other_data():
  35. """Demonstrates displaying different variables through shade and color."""
  36. y, x = np.mgrid[-4:2:200j, -4:2:200j]
  37. z1 = np.sin(x**2) # Data to hillshade
  38. z2 = np.cos(x**2 + y**2) # Data to color
  39. norm = Normalize(z2.min(), z2.max())
  40. cmap = plt.cm.RdBu
  41. ls = LightSource(315, 45)
  42. rgb = ls.shade_rgb(cmap(norm(z2)), z1)
  43. fig, ax = plt.subplots()
  44. ax.imshow(rgb, interpolation='bilinear')
  45. ax.set_title('Shade by one variable, color by another', size='x-large')
  46. display_colorbar()
  47. avoid_outliers()
  48. shade_other_data()
  49. plt.show()

下载这个示例