Python-Matplotlib:定义单个图例条目

问题描述 投票:1回答:1

当我绘制以下内容时:

plt.plot([0,1],[0,1],'g',label='Solid')
plt.plot([0,1],[.5,1],'b',label='Solid')
plt.plot([0,1],[1,0],'g--',label='Dashed')
plt.plot([0,1],[.5,0],'b--',label='Dashed')
plt.legend()

我得到这张图片:

enter image description here

对我来说,这是太多的图例文字。有谁知道,我如何才能将蓝色和绿色的实线以及蓝色和绿色的虚线连接起来,以使图例减少为带有绿色/蓝色(最好是一个在另一个顶部)和相应文本的两个条目?感谢您的帮助

python matplotlib legend legend-properties
1个回答
0
投票

查看legend(),即legend()的可能签名。>

legend(handles, labels)中也有很好的描述。

Legend Tutorial

line1, = plt.plot([0,1],[0,1],'g',label='Solid') line2, = plt.plot([0,1],[.5,1],'b',label='Solid') plt.plot([0,1],[1,0],'g--',label='Dashed') plt.plot([0,1],[.5,0],'b--',label='Dashed') plt.legend((line1, line2), ('green', 'blue')) plt.show()

© www.soinside.com 2019 - 2024. All rights reserved.