Errorbar not at the data

124 views Asked by At

I'm having trouble with the errorbar function in pyplot. It's drawing the errorbars at a seemingly random position and I can't figure out why.

This is what I'm using:

def PlotResults(X,Y,ErrorBarUp,ErrorBarDown,Header='',xLabel='',yLabel='',Bool=1):

    fig,ax = plt.subplots()
    plt.plot(X,Y/Y,color='r',label='1:1 - Linie')
    plt.plot(X, Y,marker='d',color='b',label='S_j/C_j')
    plt.errorbar(X,Y,xerr=None,yerr=[ErrorBarDown,ErrorBarUp],fmt='',color=c,capsize=2,elinewidth=0.5)

Using this example:

x = 1+np.arange(0,1,0.01)
y = 1+np.exp(-x)
yerr = 0.1*np.sqrt(x)+0.1
xerr = yerr-0.5
PlotResults(x,y,xerr,yerr,'Hallo','x-Achse','y-Achse',False)

I get the followingplot

The errorbars should however be on the blue points!

Any suggestions, what is going wrong?

1

There are 1 answers

0
MSeifert On BEST ANSWER

The yerr arguments should be non-negative. If they are negative they will end on the other side of your "plot", like happened in your example. The offset to your line was just ErrorBarUp.

If I just change it to use the abs() on the second y-error it works. At least it looks like it did work:

plt.errorbar(X,Y,yerr=[ErrorBarDown,abs(ErrorBarUp)],fmt='',color='c',capsize=2,elinewidth=0.5)

enter image description here