等高线图像

等高线,填充等高线和图像绘制的测试组合。 有关等高线标记,另请参见等高线演示示例。

本演示的重点是展示如何在图像上正确展示等高线,以及如何使两者按照需要定向。 特别要注意 “origin”和“extent” 关键字参数在imshow和contour中的用法。

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. from matplotlib import cm
  4. # Default delta is large because that makes it fast, and it illustrates
  5. # the correct registration between image and contours.
  6. delta = 0.5
  7. extent = (-3, 4, -4, 3)
  8. x = np.arange(-3.0, 4.001, delta)
  9. y = np.arange(-4.0, 3.001, delta)
  10. X, Y = np.meshgrid(x, y)
  11. Z1 = np.exp(-X**2 - Y**2)
  12. Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
  13. Z = (Z1 - Z2) * 2
  14. # Boost the upper limit to avoid truncation errors.
  15. levels = np.arange(-2.0, 1.601, 0.4)
  16. norm = cm.colors.Normalize(vmax=abs(Z).max(), vmin=-abs(Z).max())
  17. cmap = cm.PRGn
  18. fig, _axs = plt.subplots(nrows=2, ncols=2)
  19. fig.subplots_adjust(hspace=0.3)
  20. axs = _axs.flatten()
  21. cset1 = axs[0].contourf(X, Y, Z, levels, norm=norm,
  22. cmap=cm.get_cmap(cmap, len(levels) - 1))
  23. # It is not necessary, but for the colormap, we need only the
  24. # number of levels minus 1. To avoid discretization error, use
  25. # either this number or a large number such as the default (256).
  26. # If we want lines as well as filled regions, we need to call
  27. # contour separately; don't try to change the edgecolor or edgewidth
  28. # of the polygons in the collections returned by contourf.
  29. # Use levels output from previous call to guarantee they are the same.
  30. cset2 = axs[0].contour(X, Y, Z, cset1.levels, colors='k')
  31. # We don't really need dashed contour lines to indicate negative
  32. # regions, so let's turn them off.
  33. for c in cset2.collections:
  34. c.set_linestyle('solid')
  35. # It is easier here to make a separate call to contour than
  36. # to set up an array of colors and linewidths.
  37. # We are making a thick green line as a zero contour.
  38. # Specify the zero level as a tuple with only 0 in it.
  39. cset3 = axs[0].contour(X, Y, Z, (0,), colors='g', linewidths=2)
  40. axs[0].set_title('Filled contours')
  41. fig.colorbar(cset1, ax=axs[0])
  42. axs[1].imshow(Z, extent=extent, cmap=cmap, norm=norm)
  43. axs[1].contour(Z, levels, colors='k', origin='upper', extent=extent)
  44. axs[1].set_title("Image, origin 'upper'")
  45. axs[2].imshow(Z, origin='lower', extent=extent, cmap=cmap, norm=norm)
  46. axs[2].contour(Z, levels, colors='k', origin='lower', extent=extent)
  47. axs[2].set_title("Image, origin 'lower'")
  48. # We will use the interpolation "nearest" here to show the actual
  49. # image pixels.
  50. # Note that the contour lines don't extend to the edge of the box.
  51. # This is intentional. The Z values are defined at the center of each
  52. # image pixel (each color block on the following subplot), so the
  53. # domain that is contoured does not extend beyond these pixel centers.
  54. im = axs[3].imshow(Z, interpolation='nearest', extent=extent,
  55. cmap=cmap, norm=norm)
  56. axs[3].contour(Z, levels, colors='k', origin='image', extent=extent)
  57. ylim = axs[3].get_ylim()
  58. axs[3].set_ylim(ylim[::-1])
  59. axs[3].set_title("Origin from rc, reversed y-axis")
  60. fig.colorbar(im, ax=axs[3])
  61. fig.tight_layout()
  62. plt.show()

等高线图像示例

参考

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

  1. import matplotlib
  2. matplotlib.axes.Axes.contour
  3. matplotlib.pyplot.contour
  4. matplotlib.axes.Axes.imshow
  5. matplotlib.pyplot.imshow
  6. matplotlib.figure.Figure.colorbar
  7. matplotlib.pyplot.colorbar
  8. matplotlib.colors.Normalize

下载这个示例