From 1e230033a8a630d648f77b7da704ed230d471c5b Mon Sep 17 00:00:00 2001 From: Inception95 Date: Fri, 13 Dec 2019 16:53:57 -0800 Subject: [PATCH 1/2] Raise an ValueError when Axes.pie accepts negative values --- lib/matplotlib/axes/_axes.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 2cc918c32482..db1437ad881b 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -2921,6 +2921,10 @@ def pie(self, x, explode=None, labels=None, colors=None, "and will be removed %(removal)s; pass a 1D array instead.") x = np.atleast_1d(x.squeeze()) + for part in x: + if part < 0: + raise ValueError("wedge sizes must be non negative values") + sx = x.sum() if sx > 1: x = x / sx From d0d64ffa2d97f900c0d9f00bbf737ed355720313 Mon Sep 17 00:00:00 2001 From: Inception95 Date: Fri, 13 Dec 2019 23:13:29 -0800 Subject: [PATCH 2/2] Add test code and change negative number detect --- lib/matplotlib/axes/_axes.py | 5 ++--- lib/matplotlib/tests/test_axes.py | 7 +++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index db1437ad881b..4370f2659b6c 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -2921,9 +2921,8 @@ def pie(self, x, explode=None, labels=None, colors=None, "and will be removed %(removal)s; pass a 1D array instead.") x = np.atleast_1d(x.squeeze()) - for part in x: - if part < 0: - raise ValueError("wedge sizes must be non negative values") + if np.any(x < 0): + raise ValueError("wedge sizes must be non negative values") sx = x.sum() if sx > 1: diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index c63196056496..4fe5a2600cc9 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -6849,3 +6849,10 @@ def test_bbox_aspect_axes_init(): sizes.extend([bb.width, bb.height]) assert_allclose(sizes, sizes[0]) + + +def test_pi_get_negative_values(): + # Test the ValueError raised when feeding negative values into axes.pie + fig, ax = plt.subplots() + with pytest.raises(ValueError): + ax.pie([5, 5, -3], explode=[0, .1, .2])