diff --git a/examples/pie_and_polar_charts/pie_demo_features.py b/examples/pie_and_polar_charts/pie_demo_features.py index ae71b172c10d..420ecb897357 100644 --- a/examples/pie_and_polar_charts/pie_demo_features.py +++ b/examples/pie_and_polar_charts/pie_demo_features.py @@ -18,7 +18,6 @@ """ import matplotlib.pyplot as plt - # The slices will be ordered and plotted counter-clockwise. labels = 'Frogs', 'Hogs', 'Dogs', 'Logs' sizes = [15, 30, 45, 10] @@ -30,4 +29,31 @@ # Set aspect ratio to be equal so that pie is drawn as a circle. plt.axis('equal') +fig = plt.figure() +ax = fig.gca() +import numpy as np + +ax.pie(np.random.random(4), explode=explode, labels=labels, colors=colors, + autopct='%1.1f%%', shadow=True, startangle=90, + radius=0.25, center=(0,0),frame=True) +ax.pie(np.random.random(4), explode=explode, labels=labels, colors=colors, + autopct='%1.1f%%', shadow=True, startangle=90, + radius=0.25, center=(1,1),frame=True) +ax.pie(np.random.random(4), explode=explode, labels=labels, colors=colors, + autopct='%1.1f%%', shadow=True, startangle=90, + radius=0.25, center=(0,1),frame=True) +ax.pie(np.random.random(4), explode=explode, labels=labels, colors=colors, + autopct='%1.1f%%', shadow=True, startangle=90, + radius=0.25, center=(1,0),frame=True) + +ax.set_xticks([0,1]) +ax.set_yticks([0,1]) +ax.set_xticklabels(["Sunny","Cloudy"]) +ax.set_yticklabels(["Dry","Rainy"]) +ax.set_xlim((-0.5,1.5)) +ax.set_ylim((-0.5,1.5)) + +# Set aspect ratio to be equal so that pie is drawn as a circle. +ax.set_aspect('equal') + plt.show() diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 7301c2c93157..097e3137dd28 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -2320,7 +2320,8 @@ def stem(self, *args, **kwargs): 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): + wedgeprops=None, textprops=None, center=(0, 0), + frame=False): r""" Plot a pie chart. @@ -2330,7 +2331,8 @@ def pie(self, x, explode=None, labels=None, colors=None, colors=('b', 'g', 'r', 'c', 'm', 'y', 'k', 'w'), autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1, startangle=None, radius=None, - counterclock=True, wedgeprops=None, textprops=None) + counterclock=True, wedgeprops=None, textprops=None, + center = (0, 0), frame = False ) Make a pie chart of array *x*. The fractional area of each wedge is given by x/sum(x). If sum(x) <= 1, then the values @@ -2388,6 +2390,11 @@ def pie(self, x, explode=None, labels=None, colors=None, *textprops*: [ *None* | dict of key value pairs ] Dict of arguments to pass to the text objects. + *center*: [ (0,0) | sequence of 2 scalars ] + Center position of the chart. + + *frame*: [ *False* | *True* ] + Plot axes frame with the chart. The pie chart will probably look best if the figure and axes are square, or the Axes aspect is equal. e.g.:: @@ -2414,7 +2421,6 @@ def pie(self, x, explode=None, labels=None, colors=None, :class:`~matplotlib.text.Text` instances for the numeric labels. """ - self.set_frame_on(False) x = np.asarray(x).astype(np.float32) @@ -2431,7 +2437,6 @@ def pie(self, x, explode=None, labels=None, colors=None, if colors is None: colors = ('b', 'g', 'r', 'c', 'm', 'y', 'k', 'w') - center = 0, 0 if radius is None: radius = 1 @@ -2514,10 +2519,15 @@ def pie(self, x, explode=None, labels=None, colors=None, theta1 = theta2 i += 1 - self.set_xlim((-1.25, 1.25)) - self.set_ylim((-1.25, 1.25)) - self.set_xticks([]) - self.set_yticks([]) + if not frame: + self.set_frame_on(False) + + self.set_xlim((-1.25 + center[0], + 1.25 + center[0])) + self.set_ylim((-1.25 + center[1], + 1.25 + center[1])) + self.set_xticks([]) + self.set_yticks([]) if autopct is None: return slices, texts diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index 5bd6488824e9..6395f62f47e1 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -3060,7 +3060,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, - hold=None): + hold=None,center=(0,0),frame=False): ax = gca() # allow callers to override the hold state by passing hold=True|False washold = ax.ishold() @@ -3072,7 +3072,8 @@ def pie(x, explode=None, labels=None, colors=None, autopct=None, autopct=autopct, pctdistance=pctdistance, shadow=shadow, labeldistance=labeldistance, startangle=startangle, radius=radius, counterclock=counterclock, - wedgeprops=wedgeprops, textprops=textprops) + wedgeprops=wedgeprops, textprops=textprops, + center=center,frame=frame) draw_if_interactive() finally: ax.hold(washold) diff --git a/lib/matplotlib/tests/baseline_images/test_axes/pie_center_radius.png b/lib/matplotlib/tests/baseline_images/test_axes/pie_center_radius.png new file mode 100644 index 000000000000..5f715f697ac8 Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_axes/pie_center_radius.png differ diff --git a/lib/matplotlib/tests/baseline_images/test_axes/pie_frame_grid.png b/lib/matplotlib/tests/baseline_images/test_axes/pie_frame_grid.png new file mode 100644 index 000000000000..94562e76726b Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_axes/pie_frame_grid.png differ diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index f0017464dc92..3aca47fa0628 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -3314,6 +3314,25 @@ def test_pie_linewidth_0(): plt.axis('equal') +@image_comparison(baseline_images=['pie_center_radius'], extensions=['png']) +def test_pie_center_radius(): + # The slices will be ordered and plotted counter-clockwise. + 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, + wedgeprops={'linewidth': 0}, center=(1,2), radius=1.5) + + plt.annotate("Center point", xy=(1,2), xytext=(1,1.5), + arrowprops=dict(arrowstyle="->", + connectionstyle="arc3")) + # Set aspect ratio to be equal so that pie is drawn as a circle. + plt.axis('equal') + + @image_comparison(baseline_images=['pie_linewidth_2'], extensions=['png']) def test_pie_linewidth_2(): # The slices will be ordered and plotted counter-clockwise. @@ -3343,6 +3362,33 @@ def test_pie_ccw_true(): # Set aspect ratio to be equal so that pie is drawn as a circle. plt.axis('equal') + +@image_comparison(baseline_images=['pie_frame_grid'], extensions=['png']) +def test_pie_frame_grid(): + # The slices will be ordered and plotted counter-clockwise. + 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, + wedgeprops={'linewidth': 0}, + frame=True, center = (2,2) ) + + plt.pie(sizes[::-1], explode=explode, labels=labels, colors=colors, + autopct='%1.1f%%', shadow=True, startangle=90, + wedgeprops={'linewidth': 0}, + frame=True, center = (5,2) ) + + plt.pie(sizes, explode=explode[::-1], labels=labels, colors=colors, + autopct='%1.1f%%', shadow=True, startangle=90, + wedgeprops={'linewidth': 0}, + frame=True, center = (3,5) ) + # Set aspect ratio to be equal so that pie is drawn as a circle. + plt.axis('equal') + + @cleanup def test_margins(): # test all ways margins can be called