Skip to content

Commit 6e7bfef

Browse files
committed
Allow to not draw the labels on pie chart.
for consistency with other API it is relatively easy and desirable to be able to pass the `label` and call `legend()` after the fact; which will display the legend in a box in one of the corner. When doing so, having the label on each piece of pie does not make much sens (and may clobber the pie chart). Thus allow to use ``None`` as a value for ``labeldistance`` as a way to not draw them.
1 parent f677f36 commit 6e7bfef

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2735,8 +2735,10 @@ def pie(self, x, explode=None, labels=None, colors=None,
27352735
shadow : bool, optional, default: False
27362736
Draw a shadow beneath the pie.
27372737
2738-
labeldistance : float, optional, default: 1.1
2739-
The radial distance at which the pie labels are drawn
2738+
labeldistance : float or None, optional, default: 1.1
2739+
The radial distance at which the pie labels are drawn.
2740+
If set to ``None``, label are not drawn, but are stored for use in
2741+
``legend()``
27402742
27412743
startangle : float, optional, default: None
27422744
If not *None*, rotates the start of the pie chart by *angle*
@@ -2858,23 +2860,24 @@ def get_next_color():
28582860
shad.set_label('_nolegend_')
28592861
self.add_patch(shad)
28602862

2861-
xt = x + labeldistance * radius * math.cos(thetam)
2862-
yt = y + labeldistance * radius * math.sin(thetam)
2863-
label_alignment_h = xt > 0 and 'left' or 'right'
2864-
label_alignment_v = 'center'
2865-
label_rotation = 'horizontal'
2866-
if rotatelabels:
2867-
label_alignment_v = yt > 0 and 'bottom' or 'top'
2868-
label_rotation = np.rad2deg(thetam) + (0 if xt > 0 else 180)
2869-
props = dict(horizontalalignment=label_alignment_h,
2870-
verticalalignment=label_alignment_v,
2871-
rotation=label_rotation,
2872-
size=rcParams['xtick.labelsize'])
2873-
props.update(textprops)
2874-
2875-
t = self.text(xt, yt, label, **props)
2876-
2877-
texts.append(t)
2863+
if labeldistance is not None:
2864+
xt = x + labeldistance * radius * math.cos(thetam)
2865+
yt = y + labeldistance * radius * math.sin(thetam)
2866+
label_alignment_h = xt > 0 and 'left' or 'right'
2867+
label_alignment_v = 'center'
2868+
label_rotation = 'horizontal'
2869+
if rotatelabels:
2870+
label_alignment_v = yt > 0 and 'bottom' or 'top'
2871+
label_rotation = np.rad2deg(thetam) + (0 if xt > 0 else 180)
2872+
props = dict(horizontalalignment=label_alignment_h,
2873+
verticalalignment=label_alignment_v,
2874+
rotation=label_rotation,
2875+
size=rcParams['xtick.labelsize'])
2876+
props.update(textprops)
2877+
2878+
t = self.text(xt, yt, label, **props)
2879+
2880+
texts.append(t)
28782881

28792882
if autopct is not None:
28802883
xt = x + pctdistance * radius * math.cos(thetam)

0 commit comments

Comments
 (0)