我需要在matplotlib图例中显示一条垂直线,因为有一个特定的原因。我试图让matplotlib理解我想要一条垂直线,其中包含lines.Line2D(x,y),但这显然不起作用。
import matplotlib.pyplot as plt
from matplotlib import lines
fig, ax = plt.subplots()
ax.plot([0,0],[0,3])
lgd = []
lgd.append(lines.Line2D([0,0],[0,1], color = 'blue', label = 'Vertical line'))
plt.legend(handles = lgd)
我需要的线出现垂直,而不是传说。有人能帮忙吗?
2条答案
按热度按时间3zwjbxry1#
你可以在制作line2D对象时使用垂直线标记。有效标记的列表可以在here中找到。
nhn9ugyo2#
垂直标记所有行
如果目标是在图例中垂直标记每一行而不是水平标记每一行,则可以通过
handler_map
更新图例句柄。以微缩形式复制行
如果目标是得到图例中绘制的线的缩小版本,原则上可以使用Using a miniature version of the plotted data as the legend handle的答案。需要稍微修改一下,以考虑可能的0宽度边界框,我现在也编辑到原始答案中。在这里,它看起来像: