diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 2cc918c32482..4370f2659b6c 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -2921,6 +2921,9 @@ 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()) + if np.any(x < 0): + raise ValueError("wedge sizes must be non negative values") + sx = x.sum() if sx > 1: x = x / sx 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])