10000 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
implemented label rotation
  • Loading branch information
txxia committed Mar 6, 2017
commit bfec1150661aae4459a74a400f24c50ffb4470e8
5 changes: 5 additions & 0 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2620,6 +2620,7 @@ 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,11 +2644,15 @@ 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_rotation = 'horizontal'
if labelrotate:
label_rotation = thetamd + (0 if xt > 0 else 180)
Copy link
Member

Choose a reason for hiding this comment

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

Instead of using thetamd I think it would be better to just use np.rad2deg(thetam)


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

texts.append(t)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4241,8 +4241,8 @@ def test_pie_frame_grid():
# Set aspect ratio to be equal so that pie is drawn as a circle.
plt.axis('equal')

@image_comparison(baseline_images=['pie_ccw_true'], extensions=['png'])
def test_pie_label_rotate():
@image_comparison(baseline_images=['pie_labelrotate_true'], extensions=['png'])
def test_pie_labelrotate_true():
# The slices will be ordered and plotted counter-clockwise.
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
sizes = [15, 30, 45, 10]
Expand Down
0