quanticbolt
quanticbolt

Reputation: 117

ValueError in matplotlib even though arrarys are the same length

My arrays are as follows:

x = ['2.000000', '2.100000', '2.200000', '2.300000', '2.400000', '2.500000']
y = ['-0.876484', '-0.841230', '-0.776523', '-0.724883', '-0.656426', '-0.595879']
e = ['0.000655', '0.000851', '0.001311', '0.001642', '0.001702', '0.001709']

My code to plot with error bars is as follows:

import matplotlib.pyplot as plt

x = ['2.000000', '2.100000', '2.200000', '2.300000', '2.400000', '2.500000']
y = ['-0.876484', '-0.841230', '-0.776523', '-0.724883', '-0.656426', '-0.595879']
e = ['0.000655', '0.000851', '0.001311', '0.001642', '0.001702', '0.001709']
plt.errorbar(x, y, yerr=e, fmt='o')
plt.show()

I keep getting a ValueError saying:

ValueError: err must be a scalar, the same dimensions as x, or 2xN.

I can't understand why this is since the dimensions of all my arrays are equal.

Upvotes: 0

Views: 38

Answers (1)

Yuca
Yuca

Reputation: 6091

You are plotting strings, that's why matplotlib can't handle the shape of e. Change your data to numeric and it works: (your e is too small, that's why the error bars doesn't show)

x = [2.000000, 2.100000, 2.200000, 2.300000, 2.400000, 2.500000]
y = [-0.876484, -0.841230, -0.776523, -0.724883, -0.656426, 0.595879]
e = [0.000655, 0.000851, 0.001311, 0.001642, 0.001702, 0.001709]

plt.errorbar(x, y, yerr=e, fmt='o')
plt.show()

enter image description here

Upvotes: 1

Related Questions