From e6dd37b15d7c19a5c18dbc0678d4e79486a24966 Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Thu, 13 Oct 2022 12:40:51 +0200 Subject: [PATCH] Backport PR #24149: FIX: handle input to ax.bar that is all nan --- 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 e5e34c91257d..d8ac2bb829f3 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 d6d44bcdb283..c238009b0be1 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -8144,3 +8144,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])