绘制填充图的示例

演示填充图。

首先,用户可以使用matplotlib制作的最基本的填充图:

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. x = [0, 1, 2, 1]
  4. y = [1, 2, 1, 0]
  5. fig, ax = plt.subplots()
  6. ax.fill(x, y)
  7. plt.show()

绘制填充图的示例1

接下来,还有一些可选功能:

  • 使用单个命令的多条曲线。
  • 设置填充颜色。
  • 设置不透明度(alpha值)。
  1. x = np.linspace(0, 1.5 * np.pi, 500)
  2. y1 = np.sin(x)
  3. y2 = np.sin(3 * x)
  4. fig, ax = plt.subplots()
  5. ax.fill(x, y1, 'b', x, y2, 'r', alpha=0.3)
  6. # Outline of the region we've filled in
  7. ax.plot(x, y1, c='b', alpha=0.8)
  8. ax.plot(x, y2, c='r', alpha=0.8)
  9. ax.plot([x[0], x[-1]], [y1[0], y1[-1]], c='b', alpha=0.8)
  10. ax.plot([x[0], x[-1]], [y2[0], y2[-1]], c='r', alpha=0.8)
  11. plt.show()

绘制填充图的示例2

下载这个示例