Skip to content

Commit

Permalink
FIX: errorbar function should not accept Nx2 arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
wackywendell committed Aug 25, 2015
1 parent 1a78c32 commit 14e9a80
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
14 changes: 14 additions & 0 deletions lib/matplotlib/axes/_axes.py
Expand Up @@ -2824,6 +2824,14 @@ def xywhere(xs, ys, mask):
right = [thisx + thiserr for (thisx, thiserr)
in cbook.safezip(x, xerr[1])]
else:
# Check if xerr is scalar or symmetric. Asymmetric is handled
# above. This prevents Nx2 arrays from accidentally
# being accepted, when the user meant the 2xN transpose.
if not (len(xerr) == 1 or
(len(xerr) == len(x) and not (
iterable(xerr[0]) and len(xerr[0]) > 1))):
raise ValueError("xerr must be a scalar, the same "
"dimensions as x, or 2xN.")
# using list comps rather than arrays to preserve units
left = [thisx - thiserr for (thisx, thiserr)
in cbook.safezip(x, xerr)]
Expand Down Expand Up @@ -2882,6 +2890,12 @@ def xywhere(xs, ys, mask):
upper = [thisy + thiserr for (thisy, thiserr)
in cbook.safezip(y, yerr[1])]
else:
# Check for scalar or symmetric, as in xerr.
if not (len(yerr) == 1 or
(len(yerr) == len(y) and not (
iterable(yerr[0]) and len(yerr[0]) > 1))):
raise ValueError("yerr must be a scalar, the same "
"dimensions as y, or 2xN.")
# using list comps rather than arrays to preserve units
lower = [thisy - thiserr for (thisy, thiserr)
in cbook.safezip(y, yerr)]
Expand Down
21 changes: 20 additions & 1 deletion lib/matplotlib/tests/test_axes.py
Expand Up @@ -2038,7 +2038,8 @@ def test_errorbar():
# Now switch to a more OO interface to exercise more features.
fig, axs = plt.subplots(nrows=2, ncols=2, sharex=True)
ax = axs[0, 0]
ax.errorbar(x, y, yerr=yerr, fmt='o')
# Try a Nx1 shaped error just to check
ax.errorbar(x, y, yerr=np.reshape(yerr, (len(y), 1)), fmt='o')
ax.set_title('Vert. symmetric')

# With 4 subplots, reduce the number of axis ticks to avoid crowding.
Expand All @@ -2064,6 +2065,24 @@ def test_errorbar():

fig.suptitle('Variable errorbars')

@cleanup
def test_errorbar_shape():
fig = plt.figure()
ax = fig.gca()

x = np.arange(0.1, 4, 0.5)
y = np.exp(-x)
yerr1 = 0.1 + 0.2*np.sqrt(x)
yerr = np.vstack((yerr1, 2*yerr1)).T
xerr = 0.1 + yerr

print(np.shape(y))
print(np.shape(yerr))

assert_raises(ValueError, ax.errorbar, x, y, yerr=yerr, fmt='o')
assert_raises(ValueError, ax.errorbar, x, y, xerr=xerr, fmt='o')
assert_raises(ValueError, ax.errorbar, x, y, yerr=yerr, xerr=xerr, fmt='o')


@image_comparison(baseline_images=['errorbar_limits'])
def test_errorbar_limits():
Expand Down

0 comments on commit 14e9a80

Please sign in to comment.