matplotlib.pyplot.errorbar is throwing an error it

2019-05-31 07:22发布

问题:

I'm trying to make an errorbar plot with my data. X is a 9 element ndarray. Y and Yerr are 9x5 ndarrays. When I call:

matplotlib.pyplot.errorbar(X, Y, Yerr)

I get a ValueError: "yerr must be a scalar, the same dimensions as y, or 2xN."

But Y.shape == Yerr.shape is True.

I'm running on 64 bit Windows 7 with Spyder 2.3.8 and Python 3.5.1. Matplotlib is up to date. I've installed Visual C++ Redistributable for Visual Studio 2015.

Any ideas?

Edit: Some data.

X=numpy.array([1,2,3])
Y=numpy.array([[1,5,2],[3,6,4],[9,3,7]])
Yerr=numpy.ones_like(Y)

回答1:

Maybe by "dimension of y" the docs actually meant 1xN...

Anyway, this could work:

for y, yerr in zip(Y, Yerr):
    matplotlib.pyplot.errorbar(X, y, yerr)


回答2:

Hmmm....

By studying lines 2962-2965 of the module that raises the error we find

if len(yerr) > 1 and not ((len(yerr) == len(y) and not (iterable(yerr[0]) and len(yerr[0]) > 1)))

From the data

1 T len(yerr) > 1
2 T len(yerr) == len(y)
3 T iterable(yerr[0])
4 T len(yerr[0]) > 1
5 T 1 and not (2 and not (3 and 4)

However, this will not be triggered if the following test is not passed:

if (iterable(yerr) and len(yerr) == 2 and
                iterable(yerr[0]) and iterable(yerr[1])):
....

And it is not triggered, because len(yerr) = 3

Everything seems to check out, except for the dimensionality. This works:

X = numpy.tile([1,2,3],3)
Y = numpy.array([1,5,2,3,6,4,9,3,7])
Yerr = numpy.ones_like(Y)

I am not sure what causes the error. The "l0, = " assignment also seems a little quirky.