Contourf演示

如何使用 axes.Axes.contourf() 方法创建填充的等高线图。

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. origin = 'lower'
  4. delta = 0.025
  5. x = y = np.arange(-3.0, 3.01, delta)
  6. X, Y = np.meshgrid(x, y)
  7. Z1 = np.exp(-X**2 - Y**2)
  8. Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
  9. Z = (Z1 - Z2) * 2
  10. nr, nc = Z.shape
  11. # put NaNs in one corner:
  12. Z[-nr // 6:, -nc // 6:] = np.nan
  13. # contourf will convert these to masked
  14. Z = np.ma.array(Z)
  15. # mask another corner:
  16. Z[:nr // 6, :nc // 6] = np.ma.masked
  17. # mask a circle in the middle:
  18. interior = np.sqrt((X**2) + (Y**2)) < 0.5
  19. Z[interior] = np.ma.masked
  20. # We are using automatic selection of contour levels;
  21. # this is usually not such a good idea, because they don't
  22. # occur on nice boundaries, but we do it here for purposes
  23. # of illustration.
  24. fig1, ax2 = plt.subplots(constrained_layout=True)
  25. CS = ax2.contourf(X, Y, Z, 10, cmap=plt.cm.bone, origin=origin)
  26. # Note that in the following, we explicitly pass in a subset of
  27. # the contour levels used for the filled contours. Alternatively,
  28. # We could pass in additional levels to provide extra resolution,
  29. # or leave out the levels kwarg to use all of the original levels.
  30. CS2 = ax2.contour(CS, levels=CS.levels[::2], colors='r', origin=origin)
  31. ax2.set_title('Nonsense (3 masked regions)')
  32. ax2.set_xlabel('word length anomaly')
  33. ax2.set_ylabel('sentence length anomaly')
  34. # Make a colorbar for the ContourSet returned by the contourf call.
  35. cbar = fig1.colorbar(CS)
  36. cbar.ax.set_ylabel('verbosity coefficient')
  37. # Add the contour line levels to the colorbar
  38. cbar.add_lines(CS2)
  39. fig2, ax2 = plt.subplots(constrained_layout=True)
  40. # Now make a contour plot with the levels specified,
  41. # and with the colormap generated automatically from a list
  42. # of colors.
  43. levels = [-1.5, -1, -0.5, 0, 0.5, 1]
  44. CS3 = ax2.contourf(X, Y, Z, levels,
  45. colors=('r', 'g', 'b'),
  46. origin=origin,
  47. extend='both')
  48. # Our data range extends outside the range of levels; make
  49. # data below the lowest contour level yellow, and above the
  50. # highest level cyan:
  51. CS3.cmap.set_under('yellow')
  52. CS3.cmap.set_over('cyan')
  53. CS4 = ax2.contour(X, Y, Z, levels,
  54. colors=('k',),
  55. linewidths=(3,),
  56. origin=origin)
  57. ax2.set_title('Listed colors (3 masked regions)')
  58. ax2.clabel(CS4, fmt='%2.1f', colors='w', fontsize=14)
  59. # Notice that the colorbar command gets all the information it
  60. # needs from the ContourSet object, CS3.
  61. fig2.colorbar(CS3)
  62. # Illustrate all 4 possible "extend" settings:
  63. extends = ["neither", "both", "min", "max"]
  64. cmap = plt.cm.get_cmap("winter")
  65. cmap.set_under("magenta")
  66. cmap.set_over("yellow")
  67. # Note: contouring simply excludes masked or nan regions, so
  68. # instead of using the "bad" colormap value for them, it draws
  69. # nothing at all in them. Therefore the following would have
  70. # no effect:
  71. # cmap.set_bad("red")
  72. fig, axs = plt.subplots(2, 2, constrained_layout=True)
  73. for ax, extend in zip(axs.ravel(), extends):
  74. cs = ax.contourf(X, Y, Z, levels, cmap=cmap, extend=extend, origin=origin)
  75. fig.colorbar(cs, ax=ax, shrink=0.9)
  76. ax.set_title("extend = %s" % extend)
  77. ax.locator_params(nbins=4)
  78. plt.show()

Contourf演示

Contourf演示2

Contourf演示3

参考

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

  1. import matplotlib
  2. matplotlib.axes.Axes.contour
  3. matplotlib.pyplot.contour
  4. matplotlib.axes.Axes.contourf
  5. matplotlib.pyplot.contourf
  6. matplotlib.axes.Axes.clabel
  7. matplotlib.pyplot.clabel
  8. matplotlib.figure.Figure.colorbar
  9. matplotlib.pyplot.colorbar
  10. matplotlib.colors.Colormap
  11. matplotlib.colors.Colormap.set_bad
  12. matplotlib.colors.Colormap.set_under
  13. matplotlib.colors.Colormap.set_over

下载这个示例