diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index b46cbce39c58..81ebd7267ece 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -3303,6 +3303,9 @@ def pie(self, x, explode=None, labels=None, colors=None, if np.any(x < 0): raise ValueError("Wedge sizes 'x' must be non negative values") + if not np.all(np.isfinite(x)): + raise ValueError('Wedge sizes must be finite numbers') + sx = x.sum() if normalize: diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 70d1671cafa3..9c13ce76b2e2 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -9720,3 +9720,11 @@ def test_bar_shape_mismatch(): ) with pytest.raises(ValueError, match=error_message): plt.bar(x, height) + + +def test_pie_non_finite_values(): + fig, ax = plt.subplots() + df = [5, float('nan'), float('inf')] + + with pytest.raises(ValueError, match='Wedge sizes must be finite numbers'): + ax.pie(df, labels=['A', 'B', 'C'])