Pcolor 演示

使用pcolor()生成图像。

Pcolor允许您生成二维图像样式图。 下面我们将在Matplotlib中展示如何做到这一点。

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. from matplotlib.colors import LogNorm

一个简单的 pcolor 示例

  1. Z = np.random.rand(6, 10)
  2. fig, (ax0, ax1) = plt.subplots(2, 1)
  3. c = ax0.pcolor(Z)
  4. ax0.set_title('default: no edges')
  5. c = ax1.pcolor(Z, edgecolors='k', linewidths=4)
  6. ax1.set_title('thick edges')
  7. fig.tight_layout()
  8. plt.show()

Pcolor 演示

比较pcolor与类似的功能

Demonstrates similarities between pcolor(), pcolormesh(), imshow() and pcolorfast() for drawing quadrilateral grids.

  1. # make these smaller to increase the resolution
  2. dx, dy = 0.15, 0.05
  3. # generate 2 2d grids for the x & y bounds
  4. y, x = np.mgrid[slice(-3, 3 + dy, dy),
  5. slice(-3, 3 + dx, dx)]
  6. z = (1 - x / 2. + x ** 5 + y ** 3) * np.exp(-x ** 2 - y ** 2)
  7. # x and y are bounds, so z should be the value *inside* those bounds.
  8. # Therefore, remove the last value from the z array.
  9. z = z[:-1, :-1]
  10. z_min, z_max = -np.abs(z).max(), np.abs(z).max()
  11. fig, axs = plt.subplots(2, 2)
  12. ax = axs[0, 0]
  13. c = ax.pcolor(x, y, z, cmap='RdBu', vmin=z_min, vmax=z_max)
  14. ax.set_title('pcolor')
  15. # set the limits of the plot to the limits of the data
  16. ax.axis([x.min(), x.max(), y.min(), y.max()])
  17. fig.colorbar(c, ax=ax)
  18. ax = axs[0, 1]
  19. c = ax.pcolormesh(x, y, z, cmap='RdBu', vmin=z_min, vmax=z_max)
  20. ax.set_title('pcolormesh')
  21. # set the limits of the plot to the limits of the data
  22. ax.axis([x.min(), x.max(), y.min(), y.max()])
  23. fig.colorbar(c, ax=ax)
  24. ax = axs[1, 0]
  25. c = ax.imshow(z, cmap='RdBu', vmin=z_min, vmax=z_max,
  26. extent=[x.min(), x.max(), y.min(), y.max()],
  27. interpolation='nearest', origin='lower')
  28. ax.set_title('image (nearest)')
  29. fig.colorbar(c, ax=ax)
  30. ax = axs[1, 1]
  31. c = ax.pcolorfast(x, y, z, cmap='RdBu', vmin=z_min, vmax=z_max)
  32. ax.set_title('pcolorfast')
  33. fig.colorbar(c, ax=ax)
  34. fig.tight_layout()
  35. plt.show()

Pcolor 演示2

Pcolor具有对数刻度

以下显示了具有对数刻度的pcolor图。

  1. N = 100
  2. X, Y = np.mgrid[-3:3:complex(0, N), -2:2:complex(0, N)]
  3. # A low hump with a spike coming out.
  4. # Needs to have z/colour axis on a log scale so we see both hump and spike.
  5. # linear scale only shows the spike.
  6. Z1 = np.exp(-(X)**2 - (Y)**2)
  7. Z2 = np.exp(-(X * 10)**2 - (Y * 10)**2)
  8. Z = Z1 + 50 * Z2
  9. fig, (ax0, ax1) = plt.subplots(2, 1)
  10. c = ax0.pcolor(X, Y, Z,
  11. norm=LogNorm(vmin=Z.min(), vmax=Z.max()), cmap='PuBu_r')
  12. fig.colorbar(c, ax=ax0)
  13. c = ax1.pcolor(X, Y, Z, cmap='PuBu_r')
  14. fig.colorbar(c, ax=ax1)
  15. plt.show()

Pcolor 演示3

参考

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

  1. import matplotlib
  2. matplotlib.axes.Axes.pcolor
  3. matplotlib.pyplot.pcolor
  4. matplotlib.axes.Axes.pcolormesh
  5. matplotlib.pyplot.pcolormesh
  6. matplotlib.axes.Axes.pcolorfast
  7. matplotlib.axes.Axes.imshow
  8. matplotlib.pyplot.imshow
  9. matplotlib.figure.Figure.colorbar
  10. matplotlib.pyplot.colorbar
  11. matplotlib.colors.LogNorm

下载这个示例