2016-03-07 178 views
1

我有一个每个数据集只有一个数据点(即一个错误条)的错误条图。因此,我想在图例中也有一个错误栏符号。 单一可以通过legend(numpoints=1)来实现。在这些符号在图例errorbar,但不行,作为python matplotlib图例中的标记符号

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

正如你看到的,errorbars混合了水平线条,使感觉,当有一个以上连接错误条(使用legend(numpoints=2)或更高版本),但在我的情况下看起来很丑。

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

+0

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

回答

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() 

注:本作将在代码中绘制的所有图形更改设置。