diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index ccccae9db634..3a6ac3fb420f 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -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* @@ -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) diff --git a/lib/matplotlib/tests/baseline_images/test_axes/pie_no_label.png b/lib/matplotlib/tests/baseline_images/test_axes/pie_no_label.png new file mode 100644 index 000000000000..d2d651e421dc Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_axes/pie_no_label.png differ diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index a5b7331d3777..683113abe38d 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -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"]