色彩映射规范化边界

演示使用规范以非线性方式将颜色映射映射到数据上。

色彩映射规范化边界示例

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import matplotlib.colors as colors
  4. N = 100
  5. X, Y = np.mgrid[-3:3:complex(0, N), -2:2:complex(0, N)]
  6. Z1 = np.exp(-X**2 - Y**2)
  7. Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
  8. Z = (Z1 - Z2) * 2
  9. '''
  10. BoundaryNorm: For this one you provide the boundaries for your colors,
  11. and the Norm puts the first color in between the first pair, the
  12. second color between the second pair, etc.
  13. '''
  14. fig, ax = plt.subplots(3, 1, figsize=(8, 8))
  15. ax = ax.flatten()
  16. # even bounds gives a contour-like effect
  17. bounds = np.linspace(-1, 1, 10)
  18. norm = colors.BoundaryNorm(boundaries=bounds, ncolors=256)
  19. pcm = ax[0].pcolormesh(X, Y, Z,
  20. norm=norm,
  21. cmap='RdBu_r')
  22. fig.colorbar(pcm, ax=ax[0], extend='both', orientation='vertical')
  23. # uneven bounds changes the colormapping:
  24. bounds = np.array([-0.25, -0.125, 0, 0.5, 1])
  25. norm = colors.BoundaryNorm(boundaries=bounds, ncolors=256)
  26. pcm = ax[1].pcolormesh(X, Y, Z, norm=norm, cmap='RdBu_r')
  27. fig.colorbar(pcm, ax=ax[1], extend='both', orientation='vertical')
  28. pcm = ax[2].pcolormesh(X, Y, Z, cmap='RdBu_r', vmin=-np.max(Z))
  29. fig.colorbar(pcm, ax=ax[2], extend='both', orientation='vertical')
  30. plt.show()

下载这个示例