4141 BarContainer , ErrorbarContainer , PieContainer , StemContainer )
4242from matplotlib .text import Text
4343from matplotlib .transforms import _ScaledRotation
44+ from matplotlib ._api import UNSET as _UNSET
4445
4546_log = logging .getLogger (__name__ )
4647
@@ -3534,13 +3535,13 @@ def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None, bottom=0,
35343535 self .add_container (stem_container )
35353536 return stem_container
35363537
3537- @_api . make_keyword_only ( "3.10 " , "explode" )
3538- @ _preprocess_data ( replace_names = [ "x" , "explode" , "labels" , "colors " ])
3539- def pie (self , x , explode = None , labels = None , colors = None ,
3540- autopct = None , pctdistance = 0.6 , shadow = False , labeldistance = 1.1 ,
3541- startangle = 0 , radius = 1 , counterclock = True ,
3542- wedgeprops = None , textprops = None , center = (0 , 0 ),
3543- frame = False , rotatelabels = False , * , normalize = True , hatch = None ):
3538+ @_preprocess_data ( replace_names = [ "x " , "explode" , "labels" , "colors" ,
3539+ "wedge_labels " ])
3540+ def pie (self , x , * , explode = None , labels = None , colors = None , wedge_labels = None ,
3541+ wedge_label_distance = 0.6 , autopct = None , pctdistance = 0.6 , shadow = False ,
3542+ labeldistance = _UNSET , startangle = 0 , radius = 1 , counterclock = True ,
3543+ wedgeprops = None , textprops = None , center = (0 , 0 ), frame = False ,
3544+ rotatelabels = False , normalize = True , hatch = None ):
35443545 """
35453546 Plot a pie chart.
35463547
@@ -3560,7 +3561,13 @@ def pie(self, x, explode=None, labels=None, colors=None,
35603561 of the radius with which to offset each wedge.
35613562
35623563 labels : list, default: None
3563- A sequence of strings providing the labels for each wedge
3564+ A sequence of strings providing the legend labels for each wedge.
3565+
3566+ .. deprecated:: 3.12
3567+ In future these labels will not appear on the wedges but only
3568+ be made available for the legend (see *labeldistance* below).
3569+ To place labels on the wedges, use *wedge_labels* or the
3570+ `pie_label` method.
35643571
35653572 colors : :mpltype:`color` or list of :mpltype:`color`, default: None
35663573 A sequence of colors through which the pie chart will cycle. If
@@ -3573,12 +3580,35 @@ def pie(self, x, explode=None, labels=None, colors=None,
35733580
35743581 .. versionadded:: 3.7
35753582
3583+ wedge_labels : str or list of str, optional
3584+ A sequence of strings providing the labels for each wedge, or a format
3585+ string with ``absval`` and/or ``frac`` placeholders. For example, to label
3586+ each wedge with its value and the percentage in brackets::
3587+
3588+ wedge_labels="{absval:d} ({frac:.0%})"
3589+
3590+ For more control or to add multiple sets of labels, use `pie_label`
3591+ instead.
3592+
3593+ .. versionadded:: 3.12
3594+
3595+ wedge_label_distance : float, default: 0.6
3596+ The radial position of the wedge labels, relative to the pie radius.
3597+ Values > 1 are outside the wedge and values < 1 are inside the wedge.
3598+
3599+ .. versionadded:: 3.12
3600+
35763601 autopct : None or str or callable, default: None
35773602 If not *None*, *autopct* is a string or function used to label the
35783603 wedges with their numeric value. The label will be placed inside
35793604 the wedge. If *autopct* is a format string, the label will be
35803605 ``fmt % pct``. If *autopct* is a function, then it will be called.
35813606
3607+ .. admonition:: Discouraged
3608+
3609+ Consider using the *wedge_labels* parameter or `pie_label`
3610+ method instead.
3611+
35823612 pctdistance : float, default: 0.6
35833613 The relative distance along the radius at which the text
35843614 generated by *autopct* is drawn. To draw the text outside the pie,
@@ -3591,6 +3621,11 @@ def pie(self, x, explode=None, labels=None, colors=None,
35913621 If set to ``None``, labels are not drawn but are still stored for
35923622 use in `.legend`.
35933623
3624+ .. deprecated:: 3.12
3625+ From v3.14 *labeldistance* will default to ``None`` and will
3626+ later be removed altogether. Use *wedge_labels* and
3627+ *wedge_label_distance* or the `pie_label` method instead.
3628+
35943629 shadow : bool or dict, default: False
35953630 If bool, whether to draw a shadow beneath the pie. If dict, draw a shadow
35963631 passing the properties in the dict to `.Shadow`.
@@ -3672,8 +3707,33 @@ def pie(self, x, explode=None, labels=None, colors=None,
36723707 raise ValueError ('Cannot plot an unnormalized pie with sum(x) > 1' )
36733708 else :
36743709 fracs = x
3710+
3711+ if labeldistance is _UNSET :
3712+ # NB: when the labeldistance default changes, both labeldistance and
3713+ # rotatelabels should be deprecated for removal.
3714+ if labels is not None :
3715+ msg = (
3716+ "From %(removal)s labeldistance will default to None, so that the "
3717+ "strings provided in the labels parameter are only available for "
3718+ "the legend. Later labeldistance will be removed completely. To "
3719+ "preserve existing behavior for now, pass labeldistance=1.1. "
3720+ "Consider using the wedge_labels parameter or the pie_label method "
3721+ "instead of the labels parameter."
3722+ )
3723+ _api .warn_deprecated ("3.12" , message = msg )
3724+ labeldistance = 1.1
3725+
36753726 if labels is None :
36763727 labels = ['' ] * len (x )
3728+ else :
3729+ if wedge_labels is not None and labeldistance is not None :
3730+ raise ValueError (
3731+ 'wedge_labels is a replacement for labels when annotating the '
3732+ 'wedges, so the two should not be used together unless '
3733+ 'labeldistance is None. To add multiple sets of labels, use the '
3734+ 'pie_label method.'
3735+ )
3736+
36773737 if explode is None :
36783738 explode = [0 ] * len (x )
36793739 if len (x ) != len (labels ):
@@ -3731,11 +3791,16 @@ def get_next_color():
37313791
37323792 pc = PieContainer (slices , x , normalize )
37333793
3734- if labeldistance is None :
3794+ if wedge_labels is not None :
3795+ self .pie_label (pc , wedge_labels , distance = wedge_label_distance ,
3796+ textprops = textprops )
3797+
3798+ elif labeldistance is None :
37353799 # Insert an empty list of texts for backwards compatibility of the
37363800 # return value.
37373801 pc .add_texts ([])
3738- else :
3802+
3803+ if labeldistance is not None :
37393804 # Add labels to the wedges.
37403805 labels_textprops = {
37413806 'fontsize' : mpl .rcParams ['xtick.labelsize' ],
0 commit comments