Nils
Nils

Reputation: 930

Pyplot Errorbars with different x- and y-error

I am trying to create an errorbar plot with different x- and y-errors. Let's say i have the following data:

x = [[item%20 for item in reversed(range(50,55))] for _ in range(13)]
y = [[item%20 for item in reversed(range(20,25))] for _ in range(13)]


fig, ax = plt.subplots()
ax.set_xscale('log')
for i in range(len(x)):
    plt.errorbar(x=[x[0][i]], y=[y[0][i]], xerr=x[i][1:3], yerr=y[i][1:3], ls='None', label='B{}D{}'.format(x[i][3],y[i][4]))
plt.legend(prop={'size': 6})

Now this will create an error:

ValueError: err must be [ scalar | N, Nx1 or 2xN array-like ]

However, I do not understand this error, as my error has the shape (2, N=1), just like ma data is N=1. When I transpose my data and plot it, it works just fine, but I want to plot every datapoint with a different labelm marker and color. For me it would also be okay to plot all errorbars at once and change the colors, markers and assign a label afterwards, however I do not know how to do so. However, I believe that I am doing a simple mistake which would make that approach unnecessary.

Any help is highly appreciated :)

Upvotes: 1

Views: 1623

Answers (1)

Diziet Asahi
Diziet Asahi

Reputation: 40697

if you are plotting one point at a time, your errors should be shaped (2,1), not (2,) as they are in your code.

Furthermore, I think you had an error in the way you were getting your x,y values.

x = [[item for item in reversed(range(50,55))] for _ in range(13)]
y = [[item for item in reversed(range(20,25))] for _ in range(13)]


fig, ax = plt.subplots()
ax.set_xscale('log')
for i in range(len(x)):
    X = x[i][0]
    Y = y[i][0]
    dx = np.array(x[i][1:3]).reshape((2,1))
    dy = np.array(y[i][1:3]).reshape((2,1))
    print(X,Y,dx,dy)
    plt.errorbar(x=X, y=Y, xerr=dx, yerr=dy, ls='None', label='B{}D{}'.format(x[i][3],y[i][4]))
plt.legend(prop={'size': 6})

Upvotes: 1

Related Questions