Skip to content

Commit b12c761

Browse files
authored
Raise an ValueError when Axes.pie accepts negative values #159… (#15935)
Raise an ValueError when Axes.pie accepts negative values #15923
2 parents 9fe5f5b + d0d64ff commit b12c761

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2921,6 +2921,9 @@ def pie(self, x, explode=None, labels=None, colors=None,
29212921
"and will be removed %(removal)s; pass a 1D array instead.")
29222922
x = np.atleast_1d(x.squeeze())
29232923

2924+
if np.any(x < 0):
2925+
raise ValueError("wedge sizes must be non negative values")
2926+
29242927
sx = x.sum()
29252928
if sx > 1:
29262929
x = x / sx

lib/matplotlib/tests/test_axes.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6849,3 +6849,10 @@ def test_bbox_aspect_axes_init():
68496849
sizes.extend([bb.width, bb.height])
68506850

68516851
assert_allclose(sizes, sizes[0])
6852+
6853+
6854+
def test_pi_get_negative_values():
6855+
# Test the ValueError raised when feeding negative values into axes.pie
6856+
fig, ax = plt.subplots()
6857+
with pytest.raises(ValueError):
6858+
ax.pie([5, 5, -3], explode=[0, .1, .2])

0 commit comments

Comments
 (0)