8000 Add option to rotate labels in a pie chart (#2304) by txxia · Pull Request #8217 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Add option to rotate labels in a pie chart (#2304) #8217

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 11, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
renamed flag to 'rotatelabels'; fix vertical alignment
  • Loading branch information
txxia committed Mar 7, 2017
commit bd4f207cae57108481959e09b7192eb777cd5992
17 changes: 9 additions & 8 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2475,7 +2475,7 @@ def pie(self, x, explode=None, labels=None, colors=None,
autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1,
startangle=None, radius=None, counterclock=True,
wedgeprops=None, textprops=None, center=(0, 0),
frame=False, labelrotate=False):
frame=False, rotatelabels=False):
r"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind converting this docstring into numpydoc? It will make it much more readable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be happy to, do you want me to create a separate issue for that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, let's do this. I am going to merge this PR straight away, as everything is fine!

Plot a pie chart.

Expand Down Expand Up @@ -2542,7 +2542,7 @@ def pie(self, x, explode=None, labels=None, colors=None,
*frame*: [ *False* | *True* ]
Plot axes frame with the chart.

*labelrotate*: [ *False* | *True* ]
*rotatelabels*: [ *False* | *True* ]
Rotate each label to the angle of the corresponding slice.

The pie chart will probably look best if the figure and axes are
Expand Down Expand Up @@ -2620,7 +2620,6 @@ def get_next_color():
x, y = center
theta2 = (theta1 + frac) if counterclock else (theta1 - frac)
thetam = 2 * math.pi * 0.5 * (theta1 + theta2)
thetamd = 360 * 0.5 * (theta1 + theta2)
x += expl * math.cos(thetam)
y += expl * math.sin(thetam)

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

xt = x + labeldistance * radius * math.cos(thetam)
yt = y + labeldistance * radius * math.sin(thetam)
label_alignment = xt > 0 and 'left' or 'right'
label_alignment_h = xt > 0 and 'left' or 'right'
label_alignment_v = 'center'
label_rotation = 'horizontal'
if labelrotate:
label_rotation = thetamd + (0 if xt > 0 else 180)
if rotatelabels:
label_alignment_v = yt > 0 and 'bottom' or 'top'
label_rotation = np.rad2deg(thetam) + (0 if xt > 0 else 180)

t = self.text(xt, yt, label,
size=rcParams['xtick.labelsize'],
horizontalalignment=label_alignment,
verticalalignment='center',
horizontalalignment=label_alignment_h,
verticalalignment=label_alignment_v,
rotation=label_rotation,
**textprops)

Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3208,7 +3208,7 @@ def phase_spectrum(x, Fs=None, Fc=None, window=None, pad_to=None, sides=None,
def pie(x, explode=None, labels=None, colors=None, autopct=None,
pctdistance=0.6, shadow=False, labeldistance=1.1, startangle=None,
radius=None, counterclock=True, wedgeprops=None, textprops=None,
center=(0, 0), frame=False, labelrotate=False, hold=None, data=None):
center=(0, 0), frame=False, rotatelabels=False, hold=None, data=None):
ax = gca()
# Deprecated: allow callers to override the hold state
# by passing hold=True|False
Expand All @@ -3225,7 +3225,7 @@ def pie(x, explode=None, labels=None, colors=None, autopct=None,
labeldistance=labeldistance, startangle=startangle,
radius=radius, counterclock=counterclock,
wedgeprops=wedgeprops, textprops=textprops, center=center,
frame=frame, labelrotate=labelrotate, data=data)
frame=frame, rotatelabels=rotatelabels, data=data)
finally:
ax._hold = washold

Expand Down
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4242,17 +4242,17 @@ def test_pie_frame_grid():
plt.axis('equal')


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

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

Expand Down
0