From 7c5b99fa80a6c08bb90699ee43df58c106d35ad9 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Wed, 12 Oct 2022 17:56:12 -0400 Subject: [PATCH] FIX: handle input to ax.bar that is all nan closes #24127 --- lib/matplotlib/axes/_axes.py | 8 ++++++++ lib/matplotlib/tests/test_axes.py | 13 +++++++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index fdac0f3560f4..de4a99f71d24 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -2182,11 +2182,19 @@ def _convert_dx(dx, x0, xconv, convert): x0 = cbook._safe_first_finite(x0) except (TypeError, IndexError, KeyError): pass + except StopIteration: + # this means we found no finite element, fall back to first + # element unconditionally + x0 = cbook.safe_first_element(x0) try: x = cbook._safe_first_finite(xconv) except (TypeError, IndexError, KeyError): x = xconv + except StopIteration: + # this means we found no finite element, fall back to first + # element unconditionally + x = cbook.safe_first_element(xconv) delist = False if not np.iterable(dx): diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 9fb95cbd6869..bd7940a59673 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -8195,3 +8195,16 @@ def test_bar_leading_nan(): for b in rest: assert np.isfinite(b.xy).all() assert np.isfinite(b.get_width()) + + +@check_figures_equal(extensions=["png"]) +def test_bar_all_nan(fig_test, fig_ref): + mpl.style.use("mpl20") + ax_test = fig_test.subplots() + ax_ref = fig_ref.subplots() + + ax_test.bar([np.nan], [np.nan]) + ax_test.bar([1], [1]) + + ax_ref.bar([1], [1]).remove() + ax_ref.bar([1], [1])