8000 Fixing boilerplate and the suggested modifications in pie function · matplotlib/matplotlib@c3e057e · GitHub
[go: up one dir, main page]

Skip to content

Commit c3e057e

Browse files
raphacosta27tacaswell
authored andcommitted
Fixing boilerplate and the suggested modifications in pie function
1 parent a4378ef commit c3e057e

File tree

4 files changed

+56
-5
lines changed

4 files changed

+56
-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: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2901,7 +2901,7 @@ def pie(self, x, explode=None, labels=None, colors=None,
29012901
autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1,
29022902
startangle=0, radius=1, counterclock=True,
29032903
wedgeprops=None, textprops=None, center=(0, 0),
2904-
frame=False, rotatelabels=False):
2904+
frame=False, rotatelabels=False, *, normalize=None):
29052905
"""
29062906
Plot a pie chart.
29072907
@@ -2942,6 +2942,17 @@ def pie(self, x, explode=None, labels=None, colors=None,
29422942
shadow : bool, default: False
29432943
Draw a shadow beneath the pie.
29442944
2945+
normalize: None or bool, default: None
2946+
``pie()`` used to draw a partial pie if ``sum(x) < 1``. This
2947+
behavior is deprecated and will change to always normalizing the
2948+
values to a full pie by default. If you want to draw a partial pie,
2949+
please pass ``normalize=False`` explicitly.
2950+
When *True*, always make a full pie by normalizing x so that
2951+
``sum(x) == 1``. When *False*, make a partial pie.
2952+
When *None*, gives the current behavior and warns if
2953+
``sum(x) < 1``. Please note that passing None to this parameter is
2954+
deprecated.
2955+
29452956
labeldistance : float or None, default: 1.1
29462957
The radial distance at which the pie labels are drawn.
29472958
If set to ``None``, label are not drawn, but are stored for use in
@@ -3010,9 +3021,20 @@ def pie(self, x, explode=None, labels=None, colors=None,
30103021
raise ValueError("Wedge sizes 'x' must be non negative values")
30113022

30123023
sx = x.sum()
3013-
if sx > 1:
3014-
x = x / sx
30153024

3025+
if normalize is None:
3026+
if sx < 1:
3027+
cbook.warn_deprecated(
3028+
"3.1", message="normalize=None does not normalize if "
3029+
"the sum is less than 1 "
3030+
"but this behavior is deprecated "
3031+
"since %(since)s. After the deprecation period "
3032+
"the default value will be normalize=True. "
3033+
"To prevent normalization pass normalize=False ")
3034+
else:
3035+
normalize = True
3036+
if normalize:
3037+
x = x / sx
30163038
if labels is None:
30173039
labels = [''] * len(x)
30183040
if explode is None:

lib/matplotlib/pyplot.py

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

26522652

lib/matplotlib/tests/test_axes.py

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

0 commit comments

Comments
 (0)
0