Skip to content

Fix errorbar crash on nan values #24823

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

Closed
Closed
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
48 changes: 26 additions & 22 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3498,28 +3498,32 @@ def _upcast_err(err):

Otherwise, fallback to casting to an object array.
"""

if (
# make sure it is not a scalar
np.iterable(err) and
# and it is not empty
len(err) > 0 and
# and the first element is an array sub-class use
# safe_first_element because getitem is index-first not
# location first on pandas objects so err[0] almost always
# fails.
isinstance(cbook._safe_first_finite(err), np.ndarray)
):
# Get the type of the first element
atype = type(cbook._safe_first_finite(err))
# Promote the outer container to match the inner container
if atype is np.ndarray:
# Converts using np.asarray, because data cannot
# be directly passed to init of np.ndarray
return np.asarray(err, dtype=object)
# If atype is not np.ndarray, directly pass data to init.
# This works for types such as unyts and astropy units
return atype(err)
try:
if (
# make sure it is not a scalar
np.iterable(err) and
# and it is not empty
len(err) > 0 and
# and the first element is an array sub-class use
# safe_first_element because getitem is index-first not
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# safe_first_element because getitem is index-first not
# safe_first_finite because getitem is index-first not

# location first on pandas objects so err[0] almost
# always fails.
isinstance(cbook._safe_first_finite(err), np.ndarray)
):
# Get the type of the first element
atype = type(cbook._safe_first_finite(err))
# Promote the outer container to match the inner container
if atype is np.ndarray:
# Converts using np.asarray, because data cannot
# be directly passed to init of np.ndarray
return np.asarray(err, dtype=object)
# If atype is not np.ndarray, directly pass data to init.
# This works for types such as unyts and astropy units
return atype(err)
except StopIteration:
# this means we found no finite element, fall back to default
# case.
pass
Comment on lines +3523 to +3526
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you expect to raise this exception? Is it _safe_first_finite?

Copy link
Member

@timhoffm timhoffm Jan 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we raise a more telling error in _safe_first_finite? I think a ValueError subclass would make sense here to allow a more specific catching.

# Otherwise wrap it in an object array
return np.asarray(err, dtype=object)

Expand Down
6 changes: 6 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3981,6 +3981,12 @@ def test_errorbar_nan(fig_test, fig_ref):
ax.errorbar([4], [3], [6], fmt="C0")


def test_errorbar_allnan_error():
fig, ax = plt.subplots()
with pytest.warns(RuntimeWarning, match="All-NaN axis encountered"):
ax.errorbar([0], [0], [np.nan])


@image_comparison(['hist_stacked_stepfilled', 'hist_stacked_stepfilled'])
def test_hist_stacked_stepfilled():
# make some data
Expand Down