matplotlib中带有垂直线的图例 [英] Legend with vertical line in matplotlib

查看:96
本文介绍了matplotlib中带有垂直线的图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于特定原因,我需要在matplotlib图例中显示一条垂直线.我试图让matplotlib理解我想要与lines.Line2D(x,y)垂直的线,但这显然行不通.

I need to show a vertical line in a matplotlib legend for a specific reason. I am trying to make matplotlib understand that I want a vertical line with the lines.Line2D(x,y) but this is clearly not working.

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)

我需要线条显示为垂直,而不是图例.有人可以帮忙吗?

I need the line to appear vertical, not the legend. Can anyone help?

推荐答案

制作line2D对象时,可以使用垂直线标记.可以在此处找到有效标记的列表.

You can use the vertical line marker when making your line2D object. A list of valid markers can be found here.

import matplotlib.pyplot as plt
from matplotlib import lines

fig, ax = plt.subplots()
ax.plot([0,0],[0,3])

vertical_line = lines.Line2D([], [], color='#1f77b4', marker='|', linestyle='None',
                          markersize=10, markeredgewidth=1.5, label='Vertical line')

plt.legend(handles = [vertical_line])

plt.show()

这篇关于matplotlib中带有垂直线的图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆