From d08d9ad833d2bbeb4c53332145236edd3968066e Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Fri, 19 Nov 2021 09:46:54 +0100 Subject: [PATCH] Simplify test for negative xerr/yerr. 1) Move the test into x/y loop, to write it just once. 2) Change the test to directly also work for timedelta, rather than having to manually test it. (In practice, the relevant question is whether the "low" end of the errorbar is indeed below the "high" end of the errorbar.) --- lib/matplotlib/axes/_axes.py | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 6f6db320fea7..51b3a31401c7 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -3,7 +3,6 @@ import logging import math from numbers import Integral, Number -from datetime import timedelta import numpy as np from numpy import ma @@ -3291,19 +3290,6 @@ def errorbar(self, x, y, yerr=None, xerr=None, if len(x) != len(y): raise ValueError("'x' and 'y' must have the same size") - def has_negative_values(array): - if array is None: - return False - try: - return np.any(array < 0) - except TypeError: # if array contains 'datetime.timedelta' types - return np.any(array < timedelta(0)) - - if has_negative_values(xerr): - raise ValueError("'xerr' must not contain negative values") - if has_negative_values(yerr): - raise ValueError("'yerr' must not contain negative values") - if isinstance(errorevery, Integral): errorevery = (0, errorevery) if isinstance(errorevery, tuple): @@ -3426,6 +3412,9 @@ def apply_mask(arrays, mask): return [array[mask] for array in arrays] f"'{dep_axis}err' (shape: {np.shape(err)}) must be a " f"scalar or a 1D or (2, n) array-like whose shape matches " f"'{dep_axis}' (shape: {np.shape(dep)})") from None + if np.any(err < -err): # like err<0, but also works for timedelta. + raise ValueError( + f"'{dep_axis}err' must not contain negative values") # This is like # elow, ehigh = np.broadcast_to(...) # return dep - elow * ~lolims, dep + ehigh * ~uplims