Skip to content

Commit e6dd37b

Browse files
timhoffmmeeseeksmachine
authored andcommitted
Backport PR #24149: FIX: handle input to ax.bar that is all nan
1 parent 3d2bbe3 commit e6dd37b

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2182,11 +2182,19 @@ def _convert_dx(dx, x0, xconv, convert):
21822182
x0 = cbook._safe_first_finite(x0)
21832183
except (TypeError, IndexError, KeyError):
21842184
pass
2185+
except StopIteration:
2186+
# this means we found no finite element, fall back to first
2187+
# element unconditionally
2188+
x0 = cbook.safe_first_element(x0)
21852189

21862190
try:
21872191
x = cbook._safe_first_finite(xconv)
21882192
except (TypeError, IndexError, KeyError):
21892193
x = xconv
2194+
except StopIteration:
2195+
# this means we found no finite element, fall back to first
2196+
# element unconditionally
2197+
x = cbook.safe_first_element(xconv)
21902198

21912199
delist = False
21922200
if not np.iterable(dx):

lib/matplotlib/tests/test_axes.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8144,3 +8144,16 @@ def test_bar_leading_nan():
81448144
for b in rest:
81458145
assert np.isfinite(b.xy).all()
81468146
assert np.isfinite(b.get_width())
8147+
8148+
8149+
@check_figures_equal(extensions=["png"])
8150+
def test_bar_all_nan(fig_test, fig_ref):
8151+
mpl.style.use("mpl20")
8152+
ax_test = fig_test.subplots()
8153+
ax_ref = fig_ref.subplots()
8154+
8155+
ax_test.bar([np.nan], [np.nan])
8156+
ax_test.bar([1], [1])
8157+
8158+
ax_ref.bar([1], [1]).remove()
8159+
ax_ref.bar([1], [1])

0 commit comments

Comments
 (0)