8000 Simplify test for negative xerr/yerr. · matplotlib/matplotlib@d08d9ad · GitHub
[go: up one dir, main page]

Skip to content

Commit d08d9ad

Browse files
committed
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.)
1 parent 552e078 commit d08d9ad

File tree

1 file changed

+3
-14
lines changed

1 file changed

+3
-14
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import logging
44
import math
55
from numbers import Integral, Number
6-
from datetime import timedelta
76

87
import numpy as np
98
from numpy import ma
@@ -3291,19 +3290,6 @@ def errorbar(self, x, y, yerr=None, xerr=None,
32913290
if len(x) != len(y):
32923291
raise ValueError("'x' and 'y' must have the same size")
32933292

3294-
def has_negative_values(array):
3295-
if array is None:
3296-
return False
3297-
try:
3298-
return np.any(array < 0)
3299-
except TypeError: # if array contains 'datetime.timedelta' types
3300-
return np.any(array < timedelta(0))
3301-
3302-
if has_negative_values(xerr):
3303-
raise ValueError("'xerr' must not contain negative values")
3304-
if has_negative_values(yerr):
3305-
raise ValueError("'yerr' must not contain negative values")
3306-
33073293
if isinstance(errorevery, Integral):
33083294
errorevery = (0, errorevery)
33093295
if isinstance(errorevery, tuple):
@@ -3426,6 +3412,9 @@ def apply_mask(arrays, mask): return [array[mask] for array in arrays]
34263412
f"'{dep_axis}err' (shape: {np.shape(err)}) must be a "
34273413
f"scalar or a 1D or (2, n) array-like whose shape matches "
34283414
f"'{dep_axis}' (shape: {np.shape(dep)})") from None
3415+
if np.any(err < -err): # like err<0, but also works for timedelta.
3416+
raise ValueError(
3417+
f"'{dep_axis}err' must not contain negative values")
34293418
# This is like
34303419
# elow, ehigh = np.broadcast_to(...)
34313420
# return dep - elow * ~lolims, dep + ehigh * ~uplims

0 commit comments

Comments
 (0)
0