图形图例演示

不是在每个轴上绘制图例,而是可以绘制图形的所有子轴上的所有艺术家的图例。

图形图例演示

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. fig, axs = plt.subplots(1, 2)
  4. x = np.arange(0.0, 2.0, 0.02)
  5. y1 = np.sin(2 * np.pi * x)
  6. y2 = np.exp(-x)
  7. l1, l2 = axs[0].plot(x, y1, 'rs-', x, y2, 'go')
  8. y3 = np.sin(4 * np.pi * x)
  9. y4 = np.exp(-2 * x)
  10. l3, l4 = axs[1].plot(x, y3, 'yd-', x, y4, 'k^')
  11. fig.legend((l1, l2), ('Line 1', 'Line 2'), 'upper left')
  12. fig.legend((l3, l4), ('Line 3', 'Line 4'), 'upper right')
  13. plt.tight_layout()
  14. plt.show()

下载这个示例