Skip to content

Commit 7f4b044

Browse files
authored
Merge pull request #13124 from anntzer/errorbar
Simplify parsing of errorbar input.
2 parents c9ed617 + a2595e0 commit 7f4b044

File tree

1 file changed

+20
-33
lines changed

1 file changed

+20
-33
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3180,41 +3180,28 @@ def xywhere(xs, ys, mask):
31803180
return xs, ys
31813181

31823182
def extract_err(err, data):
3183-
'''private function to compute error bars
3184-
3185-
Parameters
3186-
----------
3187-
err : iterable
3188-
xerr or yerr from errorbar
3189-
data : iterable
3190-
x or y from errorbar
3191-
'''
3192-
try:
3183+
"""
3184+
Private function to parse *err* and subtract/add it to *data*.
3185+
3186+
Both *err* and *data* are already iterables at this point.
3187+
"""
3188+
try: # Asymmetric error: pair of 1D iterables.
31933189
a, b = err
3190+
iter(a)
3191+
iter(b)
31943192
except (TypeError, ValueError):
3195-
pass
3196-
else:
3197-
if np.iterable(a) and np.iterable(b):
3198-
# using list comps rather than arrays to preserve units
3199-
low = [thisx - thiserr for thisx, thiserr
3200-
in cbook.safezip(data, a)]
3201-
high = [thisx + thiserr for thisx, thiserr
3202-
in cbook.safezip(data, b)]
3203-
return low, high
3204-
# Check if xerr is scalar or symmetric. Asymmetric is handled
3205-
# above. This prevents Nx2 arrays from accidentally
3206-
# being accepted, when the user meant the 2xN transpose.
3207-
# special case for empty lists
3208-
if len(err) > 1:
3209-
fe = cbook.safe_first_element(err)
3210-
if len(err) != len(data) or np.size(fe) > 1:
3211-
raise ValueError("err must be [ scalar | N, Nx1 "
3212-
"or 2xN array-like ]")
3213-
# using list comps rather than arrays to preserve units
3214-
low = [thisx - thiserr for thisx, thiserr
3215-
in cbook.safezip(data, err)]
3216-
high = [thisx + thiserr for thisx, thiserr
3217-
in cbook.safezip(data, err)]
3193+
a = b = err # Symmetric error: 1D iterable.
3194+
# This could just be `np.ndim(a) > 1 and np.ndim(b) > 1`, except
3195+
# for the (undocumented, but tested) support for (n, 1) arrays.
3196+
a_sh = np.shape(a)
3197+
b_sh = np.shape(b)
3198+
if (len(a_sh) > 2 or (len(a_sh) == 2 and a_sh[1] != 1)
3199+
or len(b_sh) > 2 or (len(b_sh) == 2 and b_sh[1] != 1)):
3200+
raise ValueError(
3201+
"err must be a scalar or a 1D or (2, n) array-like")
3202+
# Using list comprehensions rather than arrays to preserve units.
3203+
low = [v - e for v, e in cbook.safezip(data, a)]
3204+
high = [v + e for v, e in cbook.safezip(data, b)]
32183205
return low, high
32193206

32203207
if xerr is not None:

0 commit comments

Comments
 (0)