Skip to content

Allow to not draw the labels on pie chart #11806

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 3 commits into from
Jan 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
42 changes: 23 additions & 19 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2735,8 +2735,10 @@ def pie(self, x, explode=None, labels=None, colors=None,
shadow : bool, optional, default: False
Draw a shadow beneath the pie.

labeldistance : float, optional, default: 1.1
The radial distance at which the pie labels are drawn
labeldistance : float or None, optional, default: 1.1
The radial distance at which the pie labels are drawn.
If set to ``None``, label are not drawn, but are stored for use in
``legend()``

startangle : float, optional, default: None
If not *None*, rotates the start of the pie chart by *angle*
Expand Down Expand Up @@ -2858,23 +2860,25 @@ def get_next_color():
shad.set_label('_nolegend_')
self.add_patch(shad)

xt = x + labeldistance * radius * math.cos(thetam)
yt = y + labeldistance * radius * math.sin(thetam)
label_alignment_h = xt > 0 and 'left' or 'right'
label_alignment_v = 'center'
label_rotation = 'horizontal'
if rotatelabels:
label_alignment_v = yt > 0 and 'bottom' or 'top'
label_rotation = np.rad2deg(thetam) + (0 if xt > 0 else 180)
props = dict(horizontalalignment=label_alignment_h,
verticalalignment=label_alignment_v,
rotation=label_rotation,
size=rcParams['xtick.labelsize'])
props.update(textprops)

t = self.text(xt, yt, label, **props)

texts.append(t)
if labeldistance is not None:
xt = x + labeldistance * radius * math.cos(thetam)
yt = y + labeldistance * radius * math.sin(thetam)
label_alignment_h = xt > 0 and 'left' or 'right'
label_alignment_v = 'center'
label_rotation = 'horizontal'
if rotatelabels:
label_alignment_v = yt > 0 and 'bottom' or 'top'
label_rotation = np.rad2deg(thetam) + (0 if xt > 0
else 180)
props = dict(horizontalalignment=label_alignment_h,
verticalalignment=label_alignment_v,
rotation=label_rotation,
size=rcParams['xtick.labelsize'])
props.update(textprops)

t = self.text(xt, yt, label, **props)

texts.append(t)

if autopct is not None:
xt = x + pctdistance * radius * math.cos(thetam)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4740,6 +4740,20 @@ def test_pie_rotatelabels_true():
plt.axis('equal')


@image_comparison(baseline_images=['pie_no_label'], extensions=['png'])
def test_pie_nolabel_but_legend():
labels = 'Frogs', 'Hogs', '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, labeldistance=None,
rotatelabels=True)
plt.axis('equal')
plt.ylim(-1.2, 1.2)
plt.legend()


def test_pie_textprops():
data = [23, 34, 45]
labels = ["Long name 1", "Long name 2", "Long name 3"]
Expand Down