Reputation: 299
I am importing data from a file into a pandas DataFrame and I want to graph that data with errorbars. Here is my code:
y = df['PP04-O3N2SNpos log(O/H)+12'] - df['nuclear metallicity']
yerr = np.array([df['negative error!'],df['positive error!']]).T
x = df['NED calculated virgo infall distance in Kpc']
plt.errorbar(x,y,yerr,fmt = 'r^')
plt.xlabel('Distance from center in kpc')
plt.ylabel('PP04-O3N2SNpos log(O/H)+12')
plt.title('Central metallicity vs SN metallicity')
plt.show()
However, the graph I get looks like this
As you can see, the format of the errorbars on the graph is all messed up. I'm not sure why it isn't plotting like normal errorbars, but instead with lines randomly placed.
Upvotes: 2
Views: 1267
Reputation: 40697
Your problem is with the shape of your yerr
array.
The documentation says that the array should be a Nx1
array or a 2xN
array. Your yerr
array has a Nx2
shape.
replace your second line with
# create a 2xN array for the error bars
yerr = np.array([df['negative error!'],df['positive error!']])
and you should be ok
Upvotes: 3