Skip to content

Commit 65878f3

Browse files
authored
Merge pull request matplotlib#28862 from costaparas/improve-pie-chart-error
Improve pie chart error messages
2 parents 0988fda + 5c48037 commit 65878f3

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3283,9 +3283,9 @@ def pie(self, x, explode=None, labels=None, colors=None,
32833283
if explode is None:
32843284
explode = [0] * len(x)
32853285
if len(x) != len(labels):
3286-
raise ValueError("'label' must be of length 'x'")
3286+
raise ValueError(f"'labels' must be of length 'x', not {len(labels)}")
32873287
if len(x) != len(explode):
3288-
raise ValueError("'explode' must be of length 'x'")
3288+
raise ValueError(f"'explode' must be of length 'x', not {len(explode)}")
32893289
if colors is None:
32903290
get_next_color = self._get_patches_for_fill.get_next_color
32913291
else:
@@ -3298,7 +3298,7 @@ def get_next_color():
32983298

32993299
_api.check_isinstance(Real, radius=radius, startangle=startangle)
33003300
if radius <= 0:
3301-
raise ValueError(f'radius must be a positive number, not {radius}')
3301+
raise ValueError(f"'radius' must be a positive number, not {radius}")
33023302

33033303
# Starting theta1 is the start fraction of the circle
33043304
theta1 = startangle / 360

lib/matplotlib/tests/test_axes.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6149,6 +6149,27 @@ def test_pie_get_negative_values():
61496149
ax.pie([5, 5, -3], explode=[0, .1, .2])
61506150

61516151

6152+
def test_pie_invalid_explode():
6153+
# Test ValueError raised when feeding short explode list to axes.pie
6154+
fig, ax = plt.subplots()
6155+
with pytest.raises(ValueError):
6156+
ax.pie([1, 2, 3], explode=[0.1, 0.1])
6157+
6158+
6159+
def test_pie_invalid_labels():
6160+
# Test ValueError raised when feeding short labels list to axes.pie
6161+
fig, ax = plt.subplots()
6162+
with pytest.raises(ValueError):
6163+
ax.pie([1, 2, 3], labels=["One", "Two"])
6164+
6165+
6166+
def test_pie_invalid_radius():
6167+
# Test ValueError raised when feeding negative radius to axes.pie
6168+
fig, ax = plt.subplots()
6169+
with pytest.raises(ValueError):
6170+
ax.pie([1, 2, 3], radius=-5)
6171+
6172+
61526173
def test_normalize_kwarg_pie():
61536174
fig, ax = plt.subplots()
61546175
x = [0.3, 0.3, 0.1]

0 commit comments

Comments
 (0)