From a129589bb291329f00b28df3a2e1fc8b56bb93e0 Mon Sep 17 00:00:00 2001 From: AdrashDec Date: Sat, 5 Apr 2025 20:34:47 -0400 Subject: [PATCH 1/3] handled non finite values in ax.pie --- lib/matplotlib/axes/_axes.py | 4 +++- lib/matplotlib/tests/test_axes.py | 8 ++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index b46cbce39c58..131674ef05f4 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -3302,8 +3302,10 @@ 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: x = x / sx diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 70d1671cafa3..b74e10768474 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']) From 5ddcb7775ce06b9fce373e92ee791852c11f829d Mon Sep 17 00:00:00 2001 From: AdrashDec Date: Sat, 12 Apr 2025 00:01:35 -0400 Subject: [PATCH 2/3] adressed pr review --- lib/matplotlib/axes/_axes.py | 3 ++- lib/matplotlib/tests/test_axes.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 131674ef05f4..30ec2376c2d9 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -3305,7 +3305,8 @@ def pie(self, x, explode=None, labels=None, colors=None, if not np.all(np.isfinite(x)): raise ValueError('Wedge sizes must be finite numbers') - + + sx = x.sum() if normalize: x = x / sx diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index b74e10768474..9c13ce76b2e2 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -9726,5 +9726,5 @@ 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'): + with pytest.raises(ValueError, match='Wedge sizes must be finite numbers'): ax.pie(df, labels=['A', 'B', 'C']) From ca40674d1a6e071fe441b1b0155cd004e5249ff4 Mon Sep 17 00:00:00 2001 From: AdrashDec Date: Sat, 19 Apr 2025 02:50:57 -0400 Subject: [PATCH 3/3] removed white spaces --- lib/matplotlib/axes/_axes.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 30ec2376c2d9..81ebd7267ece 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -3302,10 +3302,10 @@ 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: