使用预定义标签的图例

使用图定义图例标签。

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. # Make some fake data.
  4. a = b = np.arange(0, 3, .02)
  5. c = np.exp(a)
  6. d = c[::-1]
  7. # Create plots with pre-defined labels.
  8. fig, ax = plt.subplots()
  9. ax.plot(a, c, 'k--', label='Model length')
  10. ax.plot(a, d, 'k:', label='Data length')
  11. ax.plot(a, c + d, 'k', label='Total message length')
  12. legend = ax.legend(loc='upper center', shadow=True, fontsize='x-large')
  13. # Put a nicer background color on the legend.
  14. legend.get_frame().set_facecolor('C0')
  15. plt.show()

预定义标签的示例

参考

此示例显示了以下函数、方法、类和模块的使用:

  1. import matplotlib
  2. matplotlib.axes.Axes.plot
  3. matplotlib.pyplot.plot
  4. matplotlib.axes.Axes.legend
  5. matplotlib.pyplot.legend

下载这个示例