Skip to content

BUG: ensure that errorbar does not error on masked negative errors. #29897

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 16 additions & 3 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand Down
Loading