Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 25b9377

Browse files
QuLogicmeeseeksmachine
authored andcommitted
Backport PR #29897: BUG: ensure that errorbar does not error on masked negative errors.
1 parent 34a11ab commit 25b9377

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3813,9 +3813,12 @@ def apply_mask(arrays, mask):
38133813
f"'{dep_axis}err' must not contain None. "
38143814
"Use NaN if you want to skip a value.")
38153815

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

lib/matplotlib/tests/test_axes.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4438,10 +4438,23 @@ def test_errorbar_nan(fig_test, fig_ref):
44384438
xs = range(5)
44394439
ys = np.array([1, 2, np.nan, np.nan, 3])
44404440
es = np.array([4, 5, np.nan, np.nan, 6])
4441-
ax.errorbar(xs, ys, es)
4441+
ax.errorbar(xs, ys, yerr=es)
44424442
ax = fig_ref.add_subplot()
4443-
ax.errorbar([0, 1], [1, 2], [4, 5])
4444-
ax.errorbar([4], [3], [6], fmt="C0")
4443+
ax.errorbar([0, 1], [1, 2], yerr=[4, 5])
4444+
ax.errorbar([4], [3], yerr=[6], fmt="C0")
4445+
4446+
4447+
@check_figures_equal()
4448+
def test_errorbar_masked_negative(fig_test, fig_ref):
4449+
ax = fig_test.add_subplot()
4450+
xs = range(5)
4451+
mask = np.array([False, False, True, True, False])
4452+
ys = np.ma.array([1, 2, 2, 2, 3], mask=mask)
4453+
es = np.ma.array([4, 5, -1, -10, 6], mask=mask)
4454+
ax.errorbar(xs, ys, yerr=es)
4455+
ax = fig_ref.add_subplot()
4456+
ax.errorbar([0, 1], [1, 2], yerr=[4, 5])
4457+
ax.errorbar([4], [3], yerr=[6], fmt="C0")
44454458

44464459

44474460
@image_comparison(['hist_stacked_stepfilled', 'hist_stacked_stepfilled'])

0 commit comments

Comments
 (0)