Contourf 影线法

演示填充轮廓图形与阴影模式。

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. # invent some numbers, turning the x and y arrays into simple
  4. # 2d arrays, which make combining them together easier.
  5. x = np.linspace(-3, 5, 150).reshape(1, -1)
  6. y = np.linspace(-3, 5, 120).reshape(-1, 1)
  7. z = np.cos(x) + np.sin(y)
  8. # we no longer need x and y to be 2 dimensional, so flatten them.
  9. x, y = x.flatten(), y.flatten()

图1:最简单的带彩色条的阴影图

  1. fig1, ax1 = plt.subplots()
  2. cs = ax1.contourf(x, y, z, hatches=['-', '/', '\\', '//'],
  3. cmap='gray', extend='both', alpha=0.5)
  4. fig1.colorbar(cs)

Contourf 影线法

绘制2:没有带图例的颜色的阴影图

  1. fig2, ax2 = plt.subplots()
  2. n_levels = 6
  3. ax2.contour(x, y, z, n_levels, colors='black', linestyles='-')
  4. cs = ax2.contourf(x, y, z, n_levels, colors='none',
  5. hatches=['.', '/', '\\', None, '\\\\', '*'],
  6. extend='lower')
  7. # create a legend for the contour set
  8. artists, labels = cs.legend_elements()
  9. ax2.legend(artists, labels, handleheight=2)
  10. plt.show()

Contourf 影线法2

参考

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

  1. import matplotlib
  2. matplotlib.axes.Axes.contour
  3. matplotlib.pyplot.contour
  4. matplotlib.axes.Axes.contourf
  5. matplotlib.pyplot.contourf
  6. matplotlib.figure.Figure.colorbar
  7. matplotlib.pyplot.colorbar
  8. matplotlib.axes.Axes.legend
  9. matplotlib.pyplot.legend
  10. matplotlib.contour.ContourSet
  11. matplotlib.contour.ContourSet.legend_elements

下载这个示例