Skip to content

Added center and frame arguments for pie-charts [merge to master at cl] #3433

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

Closed
wants to merge 5 commits into from
Closed
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
28 changes: 27 additions & 1 deletion examples/pie_and_polar_charts/pie_demo_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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()
26 changes: 18 additions & 8 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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
Expand Down Expand Up @@ -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.::
Expand All @@ -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)

Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 46 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down