Skip to content

Commit 90efcbf

Browse files
authored
Merge pull request #8217 from TeamArmstrong/fix-2304
Add option to rotate labels in a pie chart (#2304)
2 parents 096b1d0 + ab44e37 commit 90efcbf

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed

lib/matplotlib/axes/_axes.py

+13-4
Original file line numberDiff line numberDiff line change
@@ -2475,7 +2475,7 @@ def pie(self, x, explode=None, labels=None, colors=None,
24752475
autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1,
24762476
startangle=None, radius=None, counterclock=True,
24772477
wedgeprops=None, textprops=None, center=(0, 0),
2478-
frame=False):
2478+
frame=False, rotatelabels=False):
24792479
r"""
24802480
Plot a pie chart.
24812481
@@ -2542,6 +2542,9 @@ def pie(self, x, explode=None, labels=None, colors=None,
25422542
*frame*: [ *False* | *True* ]
25432543
Plot axes frame with the chart.
25442544
2545+
*rotatelabels*: [ *False* | *True* ]
2546+
Rotate each label to the angle of the corresponding slice.
2547+
25452548
The pie chart will probably look best if the figure and axes are
25462549
square, or the Axes aspect is equal. e.g.::
25472550
@@ -2639,12 +2642,18 @@ def get_next_color():
26392642

26402643
xt = x + labeldistance * radius * math.cos(thetam)
26412644
yt = y + labeldistance * radius * math.sin(thetam)
2642-
label_alignment = xt > 0 and 'left' or 'right'
2645+
label_alignment_h = xt > 0 and 'left' or 'right'
2646+
label_alignment_v = 'center'
2647+
label_rotation = 'horizontal'
2648+
if rotatelabels:
2649+
label_alignment_v = yt > 0 and 'bottom' or 'top'
2650+
label_rotation = np.rad2deg(thetam) + (0 if xt > 0 else 180)
26432651

26442652
t = self.text(xt, yt, label,
26452653
size=rcParams['xtick.labelsize'],
2646-
horizontalalignment=label_alignment,
2647-
verticalalignment='center',
2654+
horizontalalignment=label_alignment_h,
2655+
verticalalignment=label_alignment_v,
2656+
rotation=label_rotation,
26482657
**textprops)
26492658

26502659
texts.append(t)

lib/matplotlib/pyplot.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3208,7 +3208,7 @@ def phase_spectrum(x, Fs=None, Fc=None, window=None, pad_to=None, sides=None,
32083208
def pie(x, explode=None, labels=None, colors=None, autopct=None,
32093209
pctdistance=0.6, shadow=False, labeldistance=1.1, startangle=None,
32103210
radius=None, counterclock=True, wedgeprops=None, textprops=None,
3211-
center=(0, 0), frame=False, hold=None, data=None):
3211+
center=(0, 0), frame=False, rotatelabels=False, hold=None, data=None):
32123212
ax = gca()
32133213
# Deprecated: allow callers to override the hold state
32143214
# by passing hold=True|False
@@ -3225,7 +3225,7 @@ def pie(x, explode=None, labels=None, colors=None, autopct=None,
32253225
labeldistance=labeldistance, startangle=startangle,
32263226
radius=radius, counterclock=counterclock,
32273227
wedgeprops=wedgeprops, textprops=textprops, center=center,
3228-
frame=frame, data=data)
3228+
frame=frame, rotatelabels=rotatelabels, data=data)
32293229
finally:
32303230
ax._hold = washold
32313231

lib/matplotlib/tests/test_axes.py

+16
Original file line numberDiff line numberDiff line change
@@ -4267,6 +4267,22 @@ def test_pie_frame_grid():
42674267
plt.axis('equal')
42684268

42694269

4270+
@image_comparison(baseline_images=['pie_rotatelabels_true'],
4271+
extensions=['png'])
4272+
def test_pie_rotatelabels_true():
4273+
# The slices will be ordered and plotted counter-clockwise.
4274+
labels = 'Hogwarts', 'Frogs', 'Dogs', 'Logs'
4275+
sizes = [15, 30, 45, 10]
4276+
colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral']
4277+
explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs')
4278+
4279+
plt.pie(sizes, explode=explode, labels=labels, colors=colors,
4280+
autopct='%1.1f%%', shadow=True, startangle=90,
4281+
rotatelabels=True)
4282+
# Set aspect ratio to be equal so that pie is drawn as a circle.
4283+
plt.axis('equal')
4284+
4285+
42704286
@image_comparison(baseline_images=['set_get_ticklabels'], extensions=['png'])
42714287
def test_set_get_ticklabels():
42724288
# test issue 2246

0 commit comments

Comments
 (0)