Skip to content

Commit 5f33e7d

Browse files
committed
FIX: errorbar function should not accept Nx2 arrays
1 parent 1a78c32 commit 5f33e7d

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

lib/matplotlib/axes/_axes.py

+14
Original file line numberDiff line numberDiff line change
@@ -2824,6 +2824,14 @@ def xywhere(xs, ys, mask):
28242824
right = [thisx + thiserr for (thisx, thiserr)
28252825
in cbook.safezip(x, xerr[1])]
28262826
else:
2827+
# Check if xerr is scalar or symmetric. Asymmetric is handled
2828+
# above. This prevents Nx2 arrays from accidentally
2829+
# being accepted, when the user meant the 2xN transpose.
2830+
if not (len(xerr) == 1 or
2831+
(len(xerr) == len(x) and not (
2832+
iterable(xerr[0]) and len(xerr[0]) > 1))):
2833+
raise ValueError("xerr must be a scalar, the same "
2834+
"dimensions as x, or 2xN.")
28272835
# using list comps rather than arrays to preserve units
28282836
left = [thisx - thiserr for (thisx, thiserr)
28292837
in cbook.safezip(x, xerr)]
@@ -2882,6 +2890,12 @@ def xywhere(xs, ys, mask):
28822890
upper = [thisy + thiserr for (thisy, thiserr)
28832891
in cbook.safezip(y, yerr[1])]
28842892
else:
2893+
# Check for scalar or symmetric, as in xerr.
2894+
if not (len(yerr) == 1 or
2895+
(len(yerr) == len(y) and not (
2896+
iterable(yerr[0]) and len(yerr[0]) > 1))):
2897+
raise ValueError("yerr must be a scalar, the same "
2898+
"dimensions as y, or 2xN.")
28852899
# using list comps rather than arrays to preserve units
28862900
lower = [thisy - thiserr for (thisy, thiserr)
28872901
in cbook.safezip(y, yerr)]

lib/matplotlib/tests/test_axes.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -2038,7 +2038,8 @@ def test_errorbar():
20382038
# Now switch to a more OO interface to exercise more features.
20392039
fig, axs = plt.subplots(nrows=2, ncols=2, sharex=True)
20402040
ax = axs[0, 0]
2041-
ax.errorbar(x, y, yerr=yerr, fmt='o')
2041+
# Try a Nx1 shaped error just to check
2042+
ax.errorbar(x, y, yerr=np.reshape(yerr, (len(y), 1)), fmt='o')
20422043
ax.set_title('Vert. symmetric')
20432044

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

20652066
fig.suptitle('Variable errorbars')
20662067

2068+
@cleanup
2069+
def test_errorbar_shape():
2070+
fig = plt.figure()
2071+
ax = fig.gca()
2072+
2073+
x = np.arange(0.1, 4, 0.5)
2074+
y = np.exp(-x)
2075+
yerr1 = 0.1 + 0.2*np.sqrt(x)
2076+
yerr = np.vstack((yerr1, 2*yerr1)).T
2077+
xerr = 0.1 + yerr
2078+
2079+
assert_raises(ValueError, ax.errorbar, x, y, yerr=yerr, fmt='o')
2080+
assert_raises(ValueError, ax.errorbar, x, y, xerr=xerr, fmt='o')
2081+
assert_raises(ValueError, ax.errorbar, x, y, yerr=yerr, xerr=xerr, fmt='o')
2082+
20672083

20682084
@image_comparison(baseline_images=['errorbar_limits'])
20692085
def test_errorbar_limits():

0 commit comments

Comments
 (0)