在 Matplotlib 中获取单独的图片形式的图例
要将图例作为单独的图片获取,我们可以采取以下步骤:
使用 Numpy 创建 x 和 y 点。
使用 figure() 方法创建新的图形,或激活现有的关于折线图和图例图的图形。
将'~.axes.Axes' 作为子图排列的一部分添加到图形中,使用add_subplot() 方法在nrow=1、ncols=1 和index=1 处。
使用 x、y 和 y1 点创建line1 和line2 。
放置line1 和line2 的图例,设置有序标签,置于中心位置。
仅使用savefig() 方法保存带图例的图形。
示例
import numpy as np
from matplotlib import pyplot as plt
x = np.linspace(1, 100, 1000)
y = np.log(x)
y1 = np.sin(x)
fig = plt.figure("Line plot")
legendFig = plt.figure("Legend plot")
ax = fig.add_subplot(111)
line1, = ax.plot(x, y, c="red", lw=4, linestyle="dashdot")
line2, = ax.plot(x, y1, c="green", lw=1, linestyle="--")
legendFig.legend([line1, line2], ["y=log(x)", "y=sin(x)"], loc='center')
legendFig.savefig('legend.png')输出

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP