Skip to content

Commit dedc7f6

Browse files
committed
Fixing boilerplate and the suggested modifications in pie function
1 parent 98560a8 commit dedc7f6

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-5
lines changed

doc/api/next_api_changes/behaviour.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,14 @@ support for it will be dropped in a future Matplotlib release.
150150
`.font_manager.json_dump` now locks the font manager dump file
151151
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
152152
... to prevent multiple processes from writing to it at the same time.
153+
154+
`.pyplot.rgrids` and `.pyplot.thetagrids` now act as setters also when called with only kwargs
155+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
156+
Previously, keyword arguments were silently ignored when no positional
157+
arguments were given.
158+
159+
Add *normalize* keyword argument to ``Axes.pie``
160+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
161+
``pie()`` used to draw a partial pie if the sum of the values was < 1. This behavior
162+
is deprecated and will change to always normalizing the values to a full pie by default.
163+
If you want to draw a partial pie, please pass ``normalize=False`` explicitly.

lib/matplotlib/axes/_axes.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2861,7 +2861,7 @@ def pie(self, x, explode=None, labels=None, colors=None,
28612861
autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1,
28622862
startangle=None, radius=None, counterclock=True,
28632863
wedgeprops=None, textprops=None, center=(0, 0),
2864-
frame=False, rotatelabels=False):
2864+
frame=False, rotatelabels=False, *, normalize=None):
28652865
"""
28662866
Plot a pie chart.
28672867
@@ -2902,6 +2902,17 @@ def pie(self, x, explode=None, labels=None, colors=None,
29022902
shadow : bool, default: False
29032903
Draw a shadow beneath the pie.
29042904
2905+
normalize: None or bool, default: None
2906+
``pie()`` used to draw a partial pie if ``sum(x) < 1``. This
2907+
behavior is deprecated and will change to always normalizing the
2908+
values to a full pie by default. If you want to draw a partial pie,
2909+
please pass ``normalize=False`` explicitly.
2910+
When *True*, always make a full pie by normalizing x so that
2911+
``sum(x) == 1``. When *False*, make a partial pie.
2912+
When *None*, gives the current behavior and warns if
2913+
``sum(x) < 1``. Please note that passing None to this parameter is
2914+
deprecated.
2915+
29052916
labeldistance : float or None, default: 1.1
29062917
The radial distance at which the pie labels are drawn.
29072918
If set to ``None``, label are not drawn, but are stored for use in
@@ -2970,9 +2981,20 @@ def pie(self, x, explode=None, labels=None, colors=None,
29702981
raise ValueError("Wedge sizes 'x' must be non negative values")
29712982

29722983
sx = x.sum()
2973-
if sx > 1:
2974-
x = x / sx
29752984

2985+
if normalize is None:
2986+
if sx < 1:
2987+
cbook.warn_deprecated(
2988+
"3.1", message="normalize=None does not normalize if "
2989+
"the sum is less than 1 "
2990+
"but this behavior is deprecated "
2991+
"since %(since)s. After the deprecation period "
2992+
"the default value will be normalize=True. "
2993+
"To prevent normalization pass normalize=False ")
2994+
else:
2995+
normalize = True
2996+
if normalize:
2997+
x = x / sx
29762998
if labels is None:
29772999
labels = [''] * len(x)
29783000
if explode is None:

lib/matplotlib/pyplot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2650,14 +2650,14 @@ def pie(
26502650
pctdistance=0.6, shadow=False, labeldistance=1.1,
26512651
startangle=None, radius=None, counterclock=True,
26522652
wedgeprops=None, textprops=None, center=(0, 0), frame=False,
2653-
rotatelabels=False, *, data=None):
2653+
rotatelabels=False, *, normalize=None, data=None):
26542654
return gca().pie(
26552655
x, explode=explode, labels=labels, colors=colors,
26562656
autopct=autopct, pctdistance=pctdistance, shadow=shadow,
26572657
labeldistance=labeldistance, startangle=startangle,
26582658
radius=radius, counterclock=counterclock,
26592659
wedgeprops=wedgeprops, textprops=textprops, center=center,
2660-
frame=frame, rotatelabels=rotatelabels,
2660+
frame=frame, rotatelabels=rotatelabels, normalize=normalize,
26612661
**({"data": data} if data is not None else {}))
26622662

26632663

lib/matplotlib/tests/test_axes.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6420,3 +6420,18 @@ def test_polar_interpolation_steps_variable_r(fig_test, fig_ref):
64206420
l.get_path()._interpolation_steps = 100
64216421
fig_ref.add_subplot(projection="polar").plot(
64226422
np.linspace(0, np.pi/2, 101), np.linspace(1, 2, 101))
6423+
6424+
6425+
def test_normalize_kwarg_warn_pie():
6426+
fig, ax = plt.subplots()
6427+
with pytest.warns(MatplotlibDeprecationWarning):
6428+
ax.pie(x=[0], normalize=None)
6429+
6430+
6431+
def test_normalize_kwarg_pie():
6432+
fig, ax = plt.subplots()
6433+
x = [0.3, 0.3, 0.1]
6434+
t1 = ax.pie(x=x, normalize=True)
6435+
assert abs(t1[0][-1].theta2 - 360.) < 1e-3
6436+
t2 = ax.pie(x=x, normalize=False)
6437+
assert abs(t2[0][-1].theta2 - 360.) > 1e-3

0 commit comments

Comments
 (0)