Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork8.1k
ENH: introduce wedge_labels parameter forpie#29152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| ``pie`` *labels* and *labeldistance* parameters | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| Currently the *labels* parameter of `~.Axes.pie` is used both for annotating the | ||
| pie wedges directly, and for automatic legend entries. For consistency | ||
| with other plotting methods, in future *labels* will only be used for the legend. | ||
| The *labeldistance* parameter will therefore default to ``None`` from Matplotlib | ||
| 3.14, when it will also be deprecated and then removed in Matplotlib 3.16. To | ||
| preserve the existing behavior for now, set ``labeldistance=1.1``. For the longer | ||
| term, use the new *wedge_labels* parameter of `~.Axes.pie` or the `~.Axes.pie_label` | ||
| method instead of *labels*. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| New *wedge_labels* parameter for pie | ||
| ------------------------------------ | ||
| `~.Axes.pie` now accepts a *wedge_labels* parameter as a shortcut to the | ||
| `~.Axes.pie_label` method. This may be used for simple annotation of the wedges | ||
| of the pie chart. It can take | ||
| * a list of strings, similar to the existing *labels* parameter | ||
| * a format string similar to the existing *autopct* parameter except that it | ||
| uses the `str.format` method, and it can handle absolute values as well as | ||
| fractions/percentages | ||
| *wedge_labels* has an accompanying *wedge_label_distance* parameter, to control | ||
| the distance of the labels from the center of the pie. | ||
| .. plot:: | ||
| :include-source: true | ||
| :alt: Two pie charts. The chart on the left has labels 'foo' and 'bar' outside the wedges. The chart on the right has labels '1' and '2' inside the wedges. | ||
| import matplotlib.pyplot as plt | ||
| fig, (ax1, ax2) = plt.subplots(ncols=2, layout='constrained') | ||
| ax1.pie([1, 2], wedge_labels=['foo', 'bar'], wedge_label_distance=1.1) | ||
| ax2.pie([1, 2], wedge_labels='{absval:d}', wedge_label_distance=0.6) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -3514,13 +3514,13 @@ def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None, bottom=0, | ||
| self.add_container(stem_container) | ||
| returnstem_container | ||
| @_preprocess_data(replace_names=["x","explode","labels","colors", | ||
| "wedge_labels"]) | ||
| defpie(self,x,*,explode=None,labels=None,colors=None,wedge_labels=None, | ||
| wedge_label_distance=0.6,autopct=None,pctdistance=0.6,shadow=False, | ||
| labeldistance=False,startangle=0,radius=1,counterclock=True, | ||
| wedgeprops=None,textprops=None,center=(0,0),frame=False, | ||
| rotatelabels=False,normalize=True,hatch=None): | ||
| """ | ||
| Plot a pie chart. | ||
| @@ -3540,7 +3540,13 @@ def pie(self, x, explode=None, labels=None, colors=None, | ||
| of the radius with which to offset each wedge. | ||
| labels : list, default: None | ||
| A sequence of strings providing the legend labels for each wedge. | ||
| .. deprecated:: 3.12 | ||
| In future these labels will not appear on the wedges but only | ||
| be made available for the legend (see *labeldistance* below). | ||
| To place labels on the wedges, use *wedge_labels* or the | ||
| `pie_label` method. | ||
| colors : :mpltype:`color` or list of :mpltype:`color`, default: None | ||
| A sequence of colors through which the pie chart will cycle. If | ||
| @@ -3553,12 +3559,35 @@ def pie(self, x, explode=None, labels=None, colors=None, | ||
| .. versionadded:: 3.7 | ||
| wedge_labels : str or list of str, optional | ||
| A sequence of strings providing the labels for each wedge, or a format | ||
| string with ``absval`` and/or ``frac`` placeholders. For example, to label | ||
| each wedge with its value and the percentage in brackets:: | ||
| wedge_labels="{absval:d} ({frac:.0%})" | ||
| For more control or to add multiple sets of labels, use `pie_label` | ||
| instead. | ||
| .. versionadded:: 3.12 | ||
| wedge_label_distance : float, default: 0.6 | ||
| The radial position of the wedge labels, relative to the pie radius. | ||
| Values > 1 are outside the wedge and values < 1 are inside the wedge. | ||
| .. versionadded:: 3.12 | ||
| autopct : None or str or callable, default: None | ||
| If not *None*, *autopct* is a string or function used to label the | ||
| wedges with their numeric value. The label will be placed inside | ||
| the wedge. If *autopct* is a format string, the label will be | ||
| ``fmt % pct``. If *autopct* is a function, then it will be called. | ||
| .. admonition:: Discouraged | ||
| Consider using the *wedge_labels* parameter or `pie_label` | ||
| method instead. | ||
| pctdistance : float, default: 0.6 | ||
| The relative distance along the radius at which the text | ||
| generated by *autopct* is drawn. To draw the text outside the pie, | ||
| @@ -3571,6 +3600,11 @@ def pie(self, x, explode=None, labels=None, colors=None, | ||
| If set to ``None``, labels are not drawn but are still stored for | ||
| use in `.legend`. | ||
| .. deprecated:: 3.12 | ||
| From v3.14 *labeldistance* will default to ``None`` and will | ||
| later be removed altogether. Use *wedge_labels* and | ||
| *wedge_label_distance* or the `pie_label` method instead. | ||
| shadow : bool or dict, default: False | ||
| If bool, whether to draw a shadow beneath the pie. If dict, draw a shadow | ||
| passing the properties in the dict to `.Shadow`. | ||
| @@ -3654,6 +3688,26 @@ def pie(self, x, explode=None, labels=None, colors=None, | ||
| fracs=x | ||
| iflabelsisNone: | ||
| labels= ['']*len(x) | ||
| else: | ||
| ifwedge_labelsisnotNoneandlabeldistanceisnotNone: | ||
| raiseValueError( | ||
| 'wedge_labels is a replacement for labels when annotating the ' | ||
| 'wedges, so the two should not be used together. To add multiple' | ||
| 'sets of labels, use the pie_label method.' | ||
| ) | ||
| iflabeldistanceisFalse: | ||
| # NB: when the labeldistance default changes, both labeldistance and | ||
| # rotatelabels should be deprecated for removal. | ||
| msg= ( | ||
| "From %(removal)s labeldistance will default to None, so that the " | ||
| "strings provided in the labels parameter are only available for " | ||
| "the legend. Later labeldistance will be removed completely. To " | ||
| "preserve existing behavior for now, pass labeldistance=1.1. " | ||
| "Consider using the wedge_labels parameter or the pie_label method " | ||
| "instead of the labels parameter." | ||
| ) | ||
| _api.warn_deprecated("3.12",message=msg) | ||
| labeldistance=1.1 | ||
| ifexplodeisNone: | ||
| explode= [0]*len(x) | ||
| iflen(x)!=len(labels): | ||
| @@ -3711,11 +3765,16 @@ def get_next_color(): | ||
| pc=PieContainer(slices,x,normalize) | ||
| ifwedge_labelsisnotNone: | ||
| self.pie_label(pc,wedge_labels,distance=wedge_label_distance, | ||
| textprops=textprops) | ||
| eliflabeldistanceisNone: | ||
| # Insert an empty list of texts for backwards compatibility of the | ||
| # return value. | ||
| pc.add_texts([]) | ||
| iflabeldistanceisnotNone: | ||
Comment on lines +3768 to +3777 Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Do we want to prohibit Since MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Yes, good point. MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Done. | ||
| # Add labels to the wedges. | ||
| labels_textprops= { | ||
| 'fontsize':mpl.rcParams['xtick.labelsize'], | ||
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.