这就是你能做到的。左对齐的关键是使用
handletextpad=0
和
handlelength=0
. 这个
columnspacing
此处控制图例两列之间的水平间距。将句柄长度设置为0只会显示图例文本。然后,您可以根据各自的曲线最终调整图例文本的颜色。
l = plt.legend(loc='upper left', ncol=2, handlelength=0, handletextpad=0, columnspacing=0.5, fontsize=36)
handles = plt.gca().get_legend().legendHandles
for i, text in enumerate(l.get_texts()):
text.set_color(lines[i].get_color())
编辑
(根据评论)
您可以使用
lines
作为
ncol
将图例定义为
l = plt.legend(loc='upper left',ncol=len(lines), handlelength=-0.2, columnspacing=-0.2, fontsize=36)
还可以使用透明度参数删除/隐藏句柄。
alpha
通过将其设置为0
handles[i].set_alpha(0)
或者把它藏起来
handles[i].set_visible(False)