errorbar,但不是line,作为python matplotlib图例中的标记符号

errorbar,但不是line,作为python matplotlib图例中的标记符号,python,matplotlib,legend,Python,Matplotlib,Legend,我有一个errorbar图,每个数据集只有一个数据点(即一个errorbar)。因此,我希望在图例中也有一个errorbar符号。 可以通过图例(numpoints=1)实现单个。在以下代码中使用此选项: import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.errorbar(x=[0.3], y=[0.7], xerr=[0.2], marker='+', markersize=10, label='

我有一个errorbar图,每个数据集只有一个数据点(即一个errorbar)。因此,我希望在图例中也有一个errorbar符号。 可以通过
图例(numpoints=1)
实现单个。在以下代码中使用此选项:

    import matplotlib.pyplot as plt

    fig, ax = plt.subplots()

    ax.errorbar(x=[0.3], y=[0.7], xerr=[0.2], marker='+', markersize=10, label='horizontal marker line')
    ax.errorbar(x=[0.7], y=[0.3], yerr=[0.2], marker='+', markersize=10, label='is too long')

    ax.set_xlim([0,1])
    ax.set_ylim([0,1])
    ax.legend(numpoints=1) # I want only one symbol

    plt.show()
结果在图例中显示以下符号:

如您所见,错误条与水平线混合在一起,当有多个错误条要连接时(使用
legend(numpoints=2)
或更高),这是有意义的,但在我的例子中看起来很难看


如何在不丢失错误栏的情况下删除图例标记中的线条?

这是由于matplotlib中的默认设置造成的。在代码开始时,您可以通过使用
rcParams
更改设置来更改它们:

import matplotlib as mpl
import matplotlib.pyplot as plt

mpl.rcParams['legend.handlelength'] = 0
mpl.rcParams['legend.markerscale'] = 0

fig, ax = plt.subplots()

ax.errorbar(x=[0.3], y=[0.7], xerr=[0.2], marker='+', markersize=10, label='horizontal marker')
ax.errorbar(x=[0.7], y=[0.3], yerr=[0.2], marker='+', markersize=10, label='is gone')

ax.set_xlim([0,1])
ax.set_ylim([0,1])
ax.legend(numpoints=1) 
plt.show()

注意:这会更改将在代码中绘制的所有图形的设置。

下面的答案是否解决了您的问题?如果是,接受它,否则评论你还有什么问题。