diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index b46cbce39c58..82ddc53904b3 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -3756,9 +3756,12 @@ def apply_mask(arrays, mask): f"'{dep_axis}err' must not contain None. " "Use NaN if you want to skip a value.") - res = np.zeros(err.shape, dtype=bool) # Default in case of nan - if np.any(np.less(err, -err, out=res, where=(err == err))): - # like err<0, but also works for timedelta and nan. + # Raise if any errors are negative, but not if they are nan. + # To avoid nan comparisons (which lead to warnings on some + # platforms), we select with `err==err` (which is False for nan). + # Also, since datetime.timedelta cannot be compared with 0, + # we compare with the negative error instead. + if np.any((check := err[err == err]) < -check): raise ValueError( f"'{dep_axis}err' must not contain negative values") # This is like diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 70d1671cafa3..c1758d2ec3e0 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -4532,10 +4532,23 @@ def test_errorbar_nan(fig_test, fig_ref): xs = range(5) ys = np.array([1, 2, np.nan, np.nan, 3]) es = np.array([4, 5, np.nan, np.nan, 6]) - ax.errorbar(xs, ys, es) + ax.errorbar(xs, ys, yerr=es) ax = fig_ref.add_subplot() - ax.errorbar([0, 1], [1, 2], [4, 5]) - ax.errorbar([4], [3], [6], fmt="C0") + ax.errorbar([0, 1], [1, 2], yerr=[4, 5]) + ax.errorbar([4], [3], yerr=[6], fmt="C0") + + +@check_figures_equal() +def test_errorbar_masked_negative(fig_test, fig_ref): + ax = fig_test.add_subplot() + xs = range(5) + mask = np.array([False, False, True, True, False]) + ys = np.ma.array([1, 2, 2, 2, 3], mask=mask) + es = np.ma.array([4, 5, -1, -10, 6], mask=mask) + ax.errorbar(xs, ys, yerr=es) + ax = fig_ref.add_subplot() + ax.errorbar([0, 1], [1, 2], yerr=[4, 5]) + ax.errorbar([4], [3], yerr=[6], fmt="C0") @image_comparison(['hist_stacked_stepfilled.png', 'hist_stacked_stepfilled.png'])