From ec6014fe97c6f70310b3259727879595e5dc0f61 Mon Sep 17 00:00:00 2001 From: Costa Paraskevopoulos Date: Sun, 22 Sep 2024 10:18:24 +1000 Subject: [PATCH 1/2] Improve pie chart error messages Fix typo in error message, add more detail and make formatting consistent --- lib/matplotlib/axes/_axes.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index d7b649ae437f..8d38746f3773 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -3283,9 +3283,9 @@ def pie(self, x, explode=None, labels=None, colors=None, if explode is None: explode = [0] * len(x) if len(x) != len(labels): - raise ValueError("'label' must be of length 'x'") + raise ValueError(f"'labels' must be of length 'x', not {len(labels)}") if len(x) != len(explode): - raise ValueError("'explode' must be of length 'x'") + raise ValueError(f"'explode' must be of length 'x', not {len(explode)}") if colors is None: get_next_color = self._get_patches_for_fill.get_next_color else: @@ -3298,7 +3298,7 @@ def get_next_color(): _api.check_isinstance(Real, radius=radius, startangle=startangle) if radius <= 0: - raise ValueError(f'radius must be a positive number, not {radius}') + raise ValueError(f"'radius' must be a positive number, not {radius}") # Starting theta1 is the start fraction of the circle theta1 = startangle / 360 From 5c48037b338170ed222275a6485cf0d37fb78ec6 Mon Sep 17 00:00:00 2001 From: Costa Paraskevopoulos Date: Mon, 23 Sep 2024 20:29:06 +1000 Subject: [PATCH 2/2] Add unit tests --- lib/matplotlib/tests/test_axes.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 33c81c44abaf..e3877dbad7af 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -6149,6 +6149,27 @@ def test_pie_get_negative_values(): ax.pie([5, 5, -3], explode=[0, .1, .2]) +def test_pie_invalid_explode(): + # Test ValueError raised when feeding short explode list to axes.pie + fig, ax = plt.subplots() + with pytest.raises(ValueError): + ax.pie([1, 2, 3], explode=[0.1, 0.1]) + + +def test_pie_invalid_labels(): + # Test ValueError raised when feeding short labels list to axes.pie + fig, ax = plt.subplots() + with pytest.raises(ValueError): + ax.pie([1, 2, 3], labels=["One", "Two"]) + + +def test_pie_invalid_radius(): + # Test ValueError raised when feeding negative radius to axes.pie + fig, ax = plt.subplots() + with pytest.raises(ValueError): + ax.pie([1, 2, 3], radius=-5) + + def test_normalize_kwarg_pie(): fig, ax = plt.subplots() x = [0.3, 0.3, 0.1]