Vin*_*ent 2 python plot matplotlib legend
要制作自定义图例,我目前使用以下内容:
handles, labels = plt.gca().get_legend_handles_labels()
my_artist = plt.Line2D((0,1),(0,0), color = "blue", linestyle = "-", linewidth = 1)
plt.legend([handle for i,handle in enumerate(handles) if i in display]+[my_artist],
[label for i,label in enumerate(labels) if i in display]+["My legend"])
Run Code Online (Sandbox Code Playgroud)
它将在图例框中绘制一条蓝线。我想要一个蓝色的小方块而不是一条线(但比简单的标记大)。怎么做 ?
做一个代理矩形,而不是Line2D中,如果你希望它是一个正方形,忙乱与handlelength(但handlelength并handleheight适用于整个图例):
import matplotlib.pyplot as plt
handles, labels = plt.gca().get_legend_handles_labels()
my_artist = plt.Line2D((0,1),(0,0), color = "blue", linestyle = "-", linewidth = 1)
p = plt.Rectangle((0, 0), 1, 1, fc="b")
plt.legend([handle for i,handle in enumerate(handles) if i in display]+[my_artist, p],
[label for i,label in enumerate(labels) if i in display]+["Line2D", "Rectangle"],
handlelength=0.8, handleheight=0.8)
plt.show()
Run Code Online (Sandbox Code Playgroud)
(示例几乎直接来自 matplotlib 文档:图例指南。)
| 归档时间: |
|
| 查看次数: |
1985 次 |
| 最近记录: |