Skip to content

Commit 93b80e2

Browse files
authored
Merge pull request #11806 from Carreau/pie-chart
Allow to not draw the labels on pie chart
2 parents d1060a8 + 0a1e5a4 commit 93b80e2

File tree

3 files changed

+37
-19
lines changed

3 files changed

+37
-19
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2685,8 +2685,10 @@ def pie(self, x, explode=None, labels=None, colors=None,
26852685
shadow : bool, optional, default: False
26862686
Draw a shadow beneath the pie.
26872687
2688-
labeldistance : float, optional, default: 1.1
2689-
The radial distance at which the pie labels are drawn
2688+
labeldistance : float or None, optional, default: 1.1
2689+
The radial distance at which the pie labels are drawn.
2690+
If set to ``None``, label are not drawn, but are stored for use in
2691+
``legend()``
26902692
26912693
startangle : float, optional, default: None
26922694
If not *None*, rotates the start of the pie chart by *angle*
@@ -2808,23 +2810,25 @@ def get_next_color():
28082810
shad.set_label('_nolegend_')
28092811
self.add_patch(shad)
28102812

2811-
xt = x + labeldistance * radius * math.cos(thetam)
2812-
yt = y + labeldistance * radius * math.sin(thetam)
2813-
label_alignment_h = xt > 0 and 'left' or 'right'
2814-
label_alignment_v = 'center'
2815-
label_rotation = 'horizontal'
2816-
if rotatelabels:
2817-
label_alignment_v = yt > 0 and 'bottom' or 'top'
2818-
label_rotation = np.rad2deg(thetam) + (0 if xt > 0 else 180)
2819-
props = dict(horizontalalignment=label_alignment_h,
2820-
verticalalignment=label_alignment_v,
2821-
rotation=label_rotation,
2822-
size=rcParams['xtick.labelsize'])
2823-
props.update(textprops)
2824-
2825-
t = self.text(xt, yt, label, **props)
2826-
2827-
texts.append(t)
2813+
if labeldistance is not None:
2814+
xt = x + labeldistance * radius * math.cos(thetam)
2815+
yt = y + labeldistance * radius * math.sin(thetam)
2816+
label_alignment_h = xt > 0 and 'left' or 'right'
2817+
label_alignment_v = 'center'
2818+
label_rotation = 'horizontal'
2819+
if rotatelabels:
2820+
label_alignment_v = yt > 0 and 'bottom' or 'top'
2821+
label_rotation = np.rad2deg(thetam) + (0 if xt > 0
2822+
else 180)
2823+
props = dict(horizontalalignment=label_alignment_h,
2824+
verticalalignment=label_alignment_v,
2825+
rotation=label_rotation,
2826+
size=rcParams['xtick.labelsize'])
2827+
props.update(textprops)
2828+
2829+
t = self.text(xt, yt, label, **props)
2830+
2831+
texts.append(t)
28282832

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

lib/matplotlib/tests/test_axes.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4874,6 +4874,20 @@ def test_pie_rotatelabels_true():
48744874
plt.axis('equal')
48754875

48764876

4877+
@image_comparison(baseline_images=['pie_no_label'], extensions=['png'])
4878+
def test_pie_nolabel_but_legend():
4879+
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
4880+
sizes = [15, 30, 45, 10]
4881+
colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral']
4882+
explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs')
4883+
plt.pie(sizes, explode=explode, labels=labels, colors=colors,
4884+
autopct='%1.1f%%', shadow=True, startangle=90, labeldistance=None,
4885+
rotatelabels=True)
4886+
plt.axis('equal')
4887+
plt.ylim(-1.2, 1.2)
4888+
plt.legend()
4889+
4890+
48774891
def test_pie_textprops():
48784892
data = [23, 34, 45]
48794893
labels = ["Long name 1", "Long name 2", "Long name 3"]

0 commit comments

Comments
 (0)