From a26b3dfb5a0f74faff1a68696f9a51e8d3f4d4e8 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Wed, 27 Dec 2017 16:16:21 +0000 Subject: [PATCH 1/3] Make errobars work with astropy units --- lib/matplotlib/axes/_axes.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index c2549b8901a5..1cc1b683d567 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -2965,13 +2965,21 @@ def _bool_asarray_helper(d, expected): def xywhere(xs, ys, mask): """ - return xs[mask], ys[mask] where mask is True but xs and - ys are not arrays + Return xs[mask], ys[mask] where mask is True but xs and + ys are not arrays. + + If they are arrays, just return xs[mask] and ys[mask]. """ assert len(xs) == len(ys) assert len(xs) == len(mask) - xs = [thisx for thisx, b in zip(xs, mask) if b] - ys = [thisy for thisy, b in zip(ys, mask) if b] + try: + xs = xs[mask] + except TypeError: + xs = [thisx for thisx, b in zip(xs, mask) if b] + try: + ys = ys[mask] + except TypeError: + ys = [thisy for thisy, b in zip(ys, mask) if b] return xs, ys def extract_err(err, data): @@ -3005,11 +3013,8 @@ def extract_err(err, data): if (len(err) != len(data) or np.size(fe) > 1): raise ValueError("err must be [ scalar | N, Nx1 " "or 2xN array-like ]") - # using list comps rather than arrays to preserve units - low = [thisx - thiserr for (thisx, thiserr) - in cbook.safezip(data, err)] - high = [thisx + thiserr for (thisx, thiserr) - in cbook.safezip(data, err)] + low = data - err + high = data + err return low, high if xerr is not None: From 1dd14e3e87c979103e51dfde6eacfeb2fca7bdbc Mon Sep 17 00:00:00 2001 From: David Stansby Date: Wed, 27 Dec 2017 16:26:46 +0000 Subject: [PATCH 2/3] Catch type error --- lib/matplotlib/axes/_axes.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 1cc1b683d567..5a46ee371e98 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -3013,8 +3013,18 @@ def extract_err(err, data): if (len(err) != len(data) or np.size(fe) > 1): raise ValueError("err must be [ scalar | N, Nx1 " "or 2xN array-like ]") - low = data - err - high = data + err + try: + low = data - err + except TypeError: + # using list comps rather than arrays to preserve units + low = [thisx - thiserr for (thisx, thiserr) + in cbook.safezip(data, err)] + try: + high = data + err + except TypeError: + # using list comps rather than arrays to preserve units + high = [thisx + thiserr for (thisx, thiserr) + in cbook.safezip(data, err)] return low, high if xerr is not None: From ceaee9723a02ad4170d994daf7b46a915f3c2133 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Wed, 27 Dec 2017 19:50:32 +0000 Subject: [PATCH 3/3] Check for arrays instead in unit support --- lib/matplotlib/axes/_axes.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 5a46ee371e98..ac379dd2f965 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -2972,14 +2972,17 @@ def xywhere(xs, ys, mask): """ assert len(xs) == len(ys) assert len(xs) == len(mask) - try: + + if isinstance(xs, np.ndarray): xs = xs[mask] - except TypeError: + else: xs = [thisx for thisx, b in zip(xs, mask) if b] - try: + + if isinstance(ys, np.ndarray): ys = ys[mask] - except TypeError: + else: ys = [thisy for thisy, b in zip(ys, mask) if b] + return xs, ys def extract_err(err, data): @@ -3013,18 +3016,18 @@ def extract_err(err, data): if (len(err) != len(data) or np.size(fe) > 1): raise ValueError("err must be [ scalar | N, Nx1 " "or 2xN array-like ]") - try: + if isinstance(data, np.ndarray): low = data - err - except TypeError: + else: # using list comps rather than arrays to preserve units low = [thisx - thiserr for (thisx, thiserr) in cbook.safezip(data, err)] - try: + if isinstance(data, np.ndarray): high = data + err - except TypeError: + else: # using list comps rather than arrays to preserve units high = [thisx + thiserr for (thisx, thiserr) - in cbook.safezip(data, err)] + in cbook.safezip(data, err)] return low, high if xerr is not None: