QuadMesh 演示

pcolormesh 使用QuadMesh,一种更快的 pcolor 泛化,但有一些限制。

此演示说明了带有掩码数据的quadmesh中的误差。

  1. import copy
  2. from matplotlib import cm, pyplot as plt
  3. import numpy as np
  4. n = 12
  5. x = np.linspace(-1.5, 1.5, n)
  6. y = np.linspace(-1.5, 1.5, n * 2)
  7. X, Y = np.meshgrid(x, y)
  8. Qx = np.cos(Y) - np.cos(X)
  9. Qz = np.sin(Y) + np.sin(X)
  10. Z = np.sqrt(X**2 + Y**2) / 5
  11. Z = (Z - Z.min()) / (Z.max() - Z.min())
  12. # The color array can include masked values.
  13. Zm = np.ma.masked_where(np.abs(Qz) < 0.5 * np.max(Qz), Z)
  14. fig, axs = plt.subplots(nrows=1, ncols=3)
  15. axs[0].pcolormesh(Qx, Qz, Z, shading='gouraud')
  16. axs[0].set_title('Without masked values')
  17. # You can control the color of the masked region. We copy the default colormap
  18. # before modifying it.
  19. cmap = copy.copy(cm.get_cmap(plt.rcParams['image.cmap']))
  20. cmap.set_bad('y', 1.0)
  21. axs[1].pcolormesh(Qx, Qz, Zm, shading='gouraud', cmap=cmap)
  22. axs[1].set_title('With masked values')
  23. # Or use the default, which is transparent.
  24. axs[2].pcolormesh(Qx, Qz, Zm, shading='gouraud')
  25. axs[2].set_title('With masked values')
  26. fig.tight_layout()
  27. plt.show()

QuadMesh 演示

参考

下面的示例演示了以下函数和方法的使用:

  1. import matplotlib
  2. matplotlib.axes.Axes.pcolormesh
  3. matplotlib.pyplot.pcolormesh

下载这个示例