Skip to content

Commit 7304238

Browse files
authored
Merge pull request #16985 from raphacosta27/issue-#16905
Adds normalize kwarg to pie function
2 parents d03f9e5 + cdb077b commit 7304238

File tree

4 files changed

+60
-5
lines changed

4 files changed

+60
-5
lines changed

doc/api/api_changes_3.3/behaviour.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,12 @@ values are displayed with an appropriate number of significant digits even if
172172
they are much smaller or much bigger than 1. To restore the old behavior,
173173
explicitly pass a "%1.2f" as the *valfmt* parameter to `.Slider`.
174174

175+
Add *normalize* keyword argument to ``Axes.pie``
176+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
177+
``pie()`` used to draw a partial pie if the sum of the values was < 1. This behavior
178+
is deprecated and will change to always normalizing the values to a full pie by default.
179+
If you want to draw a partial pie, please pass ``normalize=False`` explicitly.
180+
175181
``table.CustomCell`` is now an alias for `.table.Cell`
176182
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
177183
All the functionality of ``CustomCell`` has been moved to its base class

lib/matplotlib/axes/_axes.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2900,7 +2900,7 @@ def pie(self, x, explode=None, labels=None, colors=None,
29002900
autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1,
29012901
startangle=0, radius=1, counterclock=True,
29022902
wedgeprops=None, textprops=None, center=(0, 0),
2903-
frame=False, rotatelabels=False):
2903+
frame=False, rotatelabels=False, *, normalize=None):
29042904
"""
29052905
Plot a pie chart.
29062906
@@ -2941,6 +2941,19 @@ def pie(self, x, explode=None, labels=None, colors=None,
29412941
shadow : bool, default: False
29422942
Draw a shadow beneath the pie.
29432943
2944+
normalize: None or bool, default: None
2945+
When *True*, always make a full pie by normalizing x so that
2946+
``sum(x) == 1``. *False* makes a partial pie if ``sum(x) <= 1``
2947+
and raises a `ValueError` for ``sum(x) > 1``.
2948+
2949+
When *None*, defaults to *True* if ``sum(x) > 0`` and *False* if
2950+
``sum(x) < 1``.
2951+
2952+
Please note that the previous default value of *None* is now
2953+
deprecated, and the default will change to *True* in the next
2954+
release. Please pass ``normalize=False`` explicitly if you want to
2955+
draw a partial pie.
2956+
29442957
labeldistance : float or None, default: 1.1
29452958
The radial distance at which the pie labels are drawn.
29462959
If set to ``None``, label are not drawn, but are stored for use in
@@ -3005,9 +3018,22 @@ def pie(self, x, explode=None, labels=None, colors=None,
30053018
raise ValueError("Wedge sizes 'x' must be non negative values")
30063019

30073020
sx = x.sum()
3008-
if sx > 1:
3009-
x = x / sx
30103021

3022+
if normalize is None:
3023+
if sx < 1:
3024+
cbook.warn_deprecated(
3025+
"3.3", message="normalize=None does not normalize "
3026+
"if the sum is less than 1 but this behavior"
3027+
"is deprecated since %(since)s until %(removal)s. "
3028+
"After the deprecation "
3029+
"period the default value will be normalize=True. "
3030+
"To prevent normalization pass normalize=False ")
3031+
else:
3032+
normalize = True
3033+
if normalize:
3034+
x = x / sx
3035+
elif sx > 1:
3036+
raise ValueError('Cannot plot an unnormalized pie with sum(x) > 1')
30113037
if labels is None:
30123038
labels = [''] * len(x)
30133039
if explode is None:

lib/matplotlib/pyplot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2642,14 +2642,14 @@ def pie(
26422642
pctdistance=0.6, shadow=False, labeldistance=1.1,
26432643
startangle=0, radius=1, counterclock=True, wedgeprops=None,
26442644
textprops=None, center=(0, 0), frame=False,
2645-
rotatelabels=False, *, data=None):
2645+
rotatelabels=False, *, normalize=None, data=None):
26462646
return gca().pie(
26472647
x, explode=explode, labels=labels, colors=colors,
26482648
autopct=autopct, pctdistance=pctdistance, shadow=shadow,
26492649
labeldistance=labeldistance, startangle=startangle,
26502650
radius=radius, counterclock=counterclock,
26512651
wedgeprops=wedgeprops, textprops=textprops, center=center,
2652-
frame=frame, rotatelabels=rotatelabels,
2652+
frame=frame, rotatelabels=rotatelabels, normalize=normalize,
26532653
**({"data": data} if data is not None else {}))
26542654

26552655

lib/matplotlib/tests/test_axes.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6141,3 +6141,26 @@ def test_ytickcolor_is_not_markercolor():
61416141
ticks = ax.yaxis.get_major_ticks()
61426142
for tick in ticks:
61436143
assert tick.tick1line.get_markeredgecolor() != 'white'
6144+
6145+
6146+
@check_figures_equal(extensions=["png"])
6147+
def test_polar_interpolation_steps_variable_r(fig_test, fig_ref):
6148+
l, = fig_test.add_subplot(projection="polar").plot([0, np.pi/2], [1, 2])
6149+
l.get_path()._interpolation_steps = 100
6150+
fig_ref.add_subplot(projection="polar").plot(
6151+
np.linspace(0, np.pi/2, 101), np.linspace(1, 2, 101))
6152+
6153+
6154+
def test_normalize_kwarg_warn_pie():
6155+
fig, ax = plt.subplots()
6156+
with pytest.warns(MatplotlibDeprecationWarning):
6157+
ax.pie(x=[0], normalize=None)
6158+
6159+
6160+
def test_normalize_kwarg_pie():
6161+
fig, ax = plt.subplots()
6162+
x = [0.3, 0.3, 0.1]
6163+
t1 = ax.pie(x=x, normalize=True)
6164+
assert abs(t1[0][-1].theta2 - 360.) < 1e-3
6165+
t2 = ax.pie(x=x, normalize=False)
6166+
assert abs(t2[0][-1].theta2 - 360.) > 1e-3

0 commit comments

Comments
 (0)