错误栏,但不是行,作为python matplotlib图例中的标记符号

时间:2016-03-07 08:02:06

标签: python matplotlib legend

我有一个错误栏图,每个数据集只有一个数据点(即一个错误栏)。因此,我想在图例中也有一个错误栏符号。 单一的可以通过legend(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()

在图例中产生这些符号: errorbars in legend are mixed up with lines

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

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

1 个答案:

答案 0 :(得分:0)

这是由于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()

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

相关问题