Skip to content

Commit 604f577

Browse files
committed
renamed flag to 'rotatelabels'; fix vertical alignment
1 parent a795aa9 commit 604f577

File tree

5 files changed

+15
-14
lines changed

5 files changed

+15
-14
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 9 additions & 8 deletions
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, labelrotate=False):
2478+
frame=False, rotatelabels=False):
24792479
r"""
24802480
Plot a pie chart.
24812481
@@ -2542,7 +2542,7 @@ def pie(self, x, explode=None, labels=None, colors=None,
25422542
*frame*: [ *False* | *True* ]
25432543
Plot axes frame with the chart.
25442544
2545-
*labelrotate*: [ *False* | *True* ]
2545+
*rotatelabels*: [ *False* | *True* ]
25462546
Rotate each label to the angle of the corresponding slice.
25472547
25482548
The pie chart will probably look best if the figure and axes are
@@ -2620,7 +2620,6 @@ def get_next_color():
26202620
x, y = center
26212621
theta2 = (theta1 + frac) if counterclock else (theta1 - frac)
26222622
thetam = 2 * math.pi * 0.5 * (theta1 + theta2)
2623-
thetamd = 360 * 0.5 * (theta1 + theta2)
26242623
x += expl * math.cos(thetam)
26252624
y += expl * math.sin(thetam)
26262625

@@ -2643,15 +2642,17 @@ def get_next_color():
26432642

26442643
xt = x + labeldistance * radius * math.cos(thetam)
26452644
yt = y + labeldistance * radius * math.sin(thetam)
2646-
label_alignment = xt > 0 and 'left' or 'right'
2645+
label_alignment_h = xt > 0 and 'left' or 'right'
2646+
label_alignment_v = 'center'
26472647
label_rotation = 'horizontal'
2648-
if labelrotate:
2649-
label_rotation = thetamd + (0 if xt > 0 else 180)
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)
26502651

26512652
t = self.text(xt, yt, label,
26522653
size=rcParams['xtick.labelsize'],
2653-
horizontalalignment=label_alignment,
2654-
verticalalignment='center',
2654+
horizontalalignment=label_alignment_h,
2655+
verticalalignment=label_alignment_v,
26552656
rotation=label_rotation,
26562657
**textprops)
26572658

lib/matplotlib/pyplot.py

Lines changed: 2 additions & 2 deletions
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, labelrotate=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, labelrotate=labelrotate, data=data)
3228+
frame=frame, rotatelabels=rotatelabels, data=data)
32293229
finally:
32303230
ax._hold = washold
32313231

Binary file not shown.

lib/matplotlib/tests/test_axes.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4242,17 +4242,17 @@ def test_pie_frame_grid():
42424242
plt.axis('equal')
42434243

42444244

4245-
@image_comparison(baseline_images=['pie_labelrotate_true'], extensions=['png'])
4246-
def test_pie_labelrotate_true():
4245+
@image_comparison(baseline_images=['pie_rotatelabels_true'], extensions=['png'])
4246+
def test_pie_rotatelabels_true():
42474247
# The slices will be ordered and plotted counter-clockwise.
4248-
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
4248+
labels = 'Hogwarts', 'Frogs', 'Dogs', 'Logs'
42494249
sizes = [15, 30, 45, 10]
42504250
colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral']
42514251
explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs')
42524252

42534253
plt.pie(sizes, explode=explode, labels=labels, colors=colors,
42544254
autopct='%1.1f%%', shadow=True, startangle=90,
4255-
labelrotate=True)
4255+
rotatelabels=True)
42564256
# Set aspect ratio to be equal so that pie is drawn as a circle.
42574257
plt.axis('equal')
42584258

0 commit comments

Comments
 (0)