Skip to content

Commit e1887b8

Browse files
authored
Merge pull request #29897 from mhvk/allow_masked_negative_errors
BUG: ensure that errorbar does not error on masked negative errors.
2 parents 5ab47bb + ebeb83a commit e1887b8

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

lib/matplotlib/axes/_axes.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -3756,9 +3756,12 @@ def apply_mask(arrays, mask):
37563756
f"'{dep_axis}err' must not contain None. "
37573757
"Use NaN if you want to skip a value.")
37583758

3759-
res = np.zeros(err.shape, dtype=bool) # Default in case of nan
3760-
if np.any(np.less(err, -err, out=res, where=(err == err))):
3761-
# like err<0, but also works for timedelta and nan.
3759+
# Raise if any errors are negative, but not if they are nan.
3760+
# To avoid nan comparisons (which lead to warnings on some
3761+
# platforms), we select with `err==err` (which is False for nan).
3762+
# Also, since datetime.timedelta cannot be compared with 0,
3763+
# we compare with the negative error instead.
3764+
if np.any((check := err[err == err]) < -check):
37623765
raise ValueError(
37633766
f"'{dep_axis}err' must not contain negative values")
37643767
# This is like

lib/matplotlib/tests/test_axes.py

+16-3
Original file line numberDiff line numberDiff line change
@@ -4532,10 +4532,23 @@ def test_errorbar_nan(fig_test, fig_ref):
45324532
xs = range(5)
45334533
ys = np.array([1, 2, np.nan, np.nan, 3])
45344534
es = np.array([4, 5, np.nan, np.nan, 6])
4535-
ax.errorbar(xs, ys, es)
4535+
ax.errorbar(xs, ys, yerr=es)
45364536
ax = fig_ref.add_subplot()
4537-
ax.errorbar([0, 1], [1, 2], [4, 5])
4538-
ax.errorbar([4], [3], [6], fmt="C0")
4537+
ax.errorbar([0, 1], [1, 2], yerr=[4, 5])
4538+
ax.errorbar([4], [3], yerr=[6], fmt="C0")
4539+
4540+
4541+
@check_figures_equal()
4542+
def test_errorbar_masked_negative(fig_test, fig_ref):
4543+
ax = fig_test.add_subplot()
4544+
xs = range(5)
4545+
mask = np.array([False, False, True, True, False])
4546+
ys = np.ma.array([1, 2, 2, 2, 3], mask=mask)
4547+
es = np.ma.array([4, 5, -1, -10, 6], mask=mask)
4548+
ax.errorbar(xs, ys, yerr=es)
4549+
ax = fig_ref.add_subplot()
4550+
ax.errorbar([0, 1], [1, 2], yerr=[4, 5])
4551+
ax.errorbar([4], [3], yerr=[6], fmt="C0")
45394552

45404553

45414554
@image_comparison(['hist_stacked_stepfilled.png', 'hist_stacked_stepfilled.png'])

0 commit comments

Comments
 (0)