diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 777ab8949166..6716c4d26e42 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -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 + # 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 # Otherwise wrap it in an object array return np.asarray(err, dtype=object) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 3c0a9712089c..87fc6af1569e 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -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