Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7c5b99f

Browse files
committed
FIX: handle input to ax.bar that is all nan
closes matplotlib#24127
1 parent b90aba4 commit 7c5b99f

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
@@ -8195,3 +8195,16 @@ def test_bar_leading_nan():
81958195
for b in rest:
81968196
assert np.isfinite(b.xy).all()
81978197
assert np.isfinite(b.get_width())
8198+
8199+
8200+
@check_figures_equal(extensions=["png"])
8201+
def test_bar_all_nan(fig_test, fig_ref):
8202+
mpl.style.use("mpl20")
8203+
ax_test = fig_test.subplots()
8204+
ax_ref = fig_ref.subplots()
8205+
8206+
ax_test.bar([np.nan], [np.nan])
8207+
ax_test.bar([1], [1])
8208+
8209+
ax_ref.bar([1], [1]).remove()
8210+
ax_ref.bar([1], [1])

0 commit comments

Comments
 (0)