Skip to content

Commit e7717f7

Browse files
committed
Merge kpalin:v1.4.x to master. Added pie kwargs
ENH : add center and frame kwargs to pie Conflicts: lib/matplotlib/tests/test_axes.py whitespace conflicts
2 parents c312da1 + 52fb9a0 commit e7717f7

File tree

6 files changed

+95
-11
lines changed

6 files changed

+95
-11
lines changed

examples/pie_and_polar_charts/pie_demo_features.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
"""
1919
import matplotlib.pyplot as plt
2020

21-
2221
# The slices will be ordered and plotted counter-clockwise.
2322
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
2423
sizes = [15, 30, 45, 10]
@@ -30,4 +29,31 @@
3029
# Set aspect ratio to be equal so that pie is drawn as a circle.
3130
plt.axis('equal')
3231

32+
fig = plt.figure()
33+
ax = fig.gca()
34+
import numpy as np
35+
36+
ax.pie(np.random.random(4), explode=explode, labels=labels, colors=colors,
37+
autopct='%1.1f%%', shadow=True, startangle=90,
38+
radius=0.25, center=(0,0),frame=True)
39+
ax.pie(np.random.random(4), explode=explode, labels=labels, colors=colors,
40+
autopct='%1.1f%%', shadow=True, startangle=90,
41+
radius=0.25, center=(1,1),frame=True)
42+
ax.pie(np.random.random(4), explode=explode, labels=labels, colors=colors,
43+
autopct='%1.1f%%', shadow=True, startangle=90,
44+
radius=0.25, center=(0,1),frame=True)
45+
ax.pie(np.random.random(4), explode=explode, labels=labels, colors=colors,
46+
autopct='%1.1f%%', shadow=True, startangle=90,
47+
radius=0.25, center=(1,0),frame=True)
48+
49+
ax.set_xticks([0,1])
50+
ax.set_yticks([0,1])
51+
ax.set_xticklabels(["Sunny","Cloudy"])
52+
ax.set_yticklabels(["Dry","Rainy"])
53+
ax.set_xlim((-0.5,1.5))
54+
ax.set_ylim((-0.5,1.5))
55+
56+
# Set aspect ratio to be equal so that pie is drawn as a circle.
57+
ax.set_aspect('equal')
58+
3359
plt.show()

lib/matplotlib/axes/_axes.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2321,7 +2321,8 @@ def stem(self, *args, **kwargs):
23212321
def pie(self, x, explode=None, labels=None, colors=None,
23222322
autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1,
23232323
startangle=None, radius=None, counterclock=True,
2324-
wedgeprops=None, textprops=None):
2324+
wedgeprops=None, textprops=None, center=(0, 0),
2325+
frame=False):
23252326
r"""
23262327
Plot a pie chart.
23272328
@@ -2331,7 +2332,8 @@ def pie(self, x, explode=None, labels=None, colors=None,
23312332
colors=('b', 'g', 'r', 'c', 'm', 'y', 'k', 'w'),
23322333
autopct=None, pctdistance=0.6, shadow=False,
23332334
labeldistance=1.1, startangle=None, radius=None,
2334-
counterclock=True, wedgeprops=None, textprops=None)
2335+
counterclock=True, wedgeprops=None, textprops=None,
2336+
center = (0, 0), frame = False )
23352337
23362338
Make a pie chart of array *x*. The fractional area of each
23372339
wedge is given by x/sum(x). If sum(x) <= 1, then the values
@@ -2389,6 +2391,11 @@ def pie(self, x, explode=None, labels=None, colors=None,
23892391
*textprops*: [ *None* | dict of key value pairs ]
23902392
Dict of arguments to pass to the text objects.
23912393
2394+
*center*: [ (0,0) | sequence of 2 scalars ]
2395+
Center position of the chart.
2396+
2397+
*frame*: [ *False* | *True* ]
2398+
Plot axes frame with the chart.
23922399
23932400
The pie chart will probably look best if the figure and axes are
23942401
square, or the Axes aspect is equal. e.g.::
@@ -2415,7 +2422,6 @@ def pie(self, x, explode=None, labels=None, colors=None,
24152422
:class:`~matplotlib.text.Text` instances for the numeric
24162423
labels.
24172424
"""
2418-
self.set_frame_on(False)
24192425

24202426
x = np.asarray(x).astype(np.float32)
24212427

@@ -2432,7 +2438,6 @@ def pie(self, x, explode=None, labels=None, colors=None,
24322438
if colors is None:
24332439
colors = ('b', 'g', 'r', 'c', 'm', 'y', 'k', 'w')
24342440

2435-
center = 0, 0
24362441
if radius is None:
24372442
radius = 1
24382443

@@ -2515,10 +2520,15 @@ def pie(self, x, explode=None, labels=None, colors=None,
25152520
theta1 = theta2
25162521
i += 1
25172522

2518-
self.set_xlim((-1.25, 1.25))
2519-
self.set_ylim((-1.25, 1.25))
2520-
self.set_xticks([])
2521-
self.set_yticks([])
2523+
if not frame:
2524+
self.set_frame_on(False)
2525+
2526+
self.set_xlim((-1.25 + center[0],
2527+
1.25 + center[0]))
2528+
self.set_ylim((-1.25 + center[1],
2529+
1.25 + center[1]))
2530+
self.set_xticks([])
2531+
self.set_yticks([])
25222532

25232533
if autopct is None:
25242534
return slices, texts

lib/matplotlib/pyplot.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3065,7 +3065,7 @@ def phase_spectrum(x, Fs=None, Fc=None, window=None, pad_to=None, sides=None,
30653065
def pie(x, explode=None, labels=None, colors=None, autopct=None,
30663066
pctdistance=0.6, shadow=False, labeldistance=1.1, startangle=None,
30673067
radius=None, counterclock=True, wedgeprops=None, textprops=None,
3068-
hold=None):
3068+
hold=None,center=(0,0),frame=False):
30693069
ax = gca()
30703070
# allow callers to override the hold state by passing hold=True|False
30713071
washold = ax.ishold()
@@ -3077,7 +3077,8 @@ def pie(x, explode=None, labels=None, colors=None, autopct=None,
30773077
autopct=autopct, pctdistance=pctdistance, shadow=shadow,
30783078
labeldistance=labeldistance, startangle=startangle,
30793079
radius=radius, counterclock=counterclock,
3080-
wedgeprops=wedgeprops, textprops=textprops)
3080+
wedgeprops=wedgeprops, textprops=textprops,
3081+
center=center,frame=frame)
30813082
draw_if_interactive()
30823083
finally:
30833084
ax.hold(washold)

lib/matplotlib/tests/test_axes.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3340,6 +3340,25 @@ def test_pie_linewidth_0():
33403340
plt.axis('equal')
33413341

33423342

3343+
@image_comparison(baseline_images=['pie_center_radius'], extensions=['png'])
3344+
def test_pie_center_radius():
3345+
# The slices will be ordered and plotted counter-clockwise.
3346+
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
3347+
sizes = [15, 30, 45, 10]
3348+
colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral']
3349+
explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs')
3350+
3351+
plt.pie(sizes, explode=explode, labels=labels, colors=colors,
3352+
autopct='%1.1f%%', shadow=True, startangle=90,
3353+
wedgeprops={'linewidth': 0}, center=(1,2), radius=1.5)
3354+
3355+
plt.annotate("Center point", xy=(1,2), xytext=(1,1.5),
3356+
arrowprops=dict(arrowstyle="->",
3357+
connectionstyle="arc3"))
3358+
# Set aspect ratio to be equal so that pie is drawn as a circle.
3359+
plt.axis('equal')
3360+
3361+
33433362
@image_comparison(baseline_images=['pie_linewidth_2'], extensions=['png'])
33443363
def test_pie_linewidth_2():
33453364
# The slices will be ordered and plotted counter-clockwise.
@@ -3370,6 +3389,34 @@ def test_pie_ccw_true():
33703389
plt.axis('equal')
33713390

33723391

3392+
@image_comparison(baseline_images=['pie_frame_grid'], extensions=['png'])
3393+
def test_pie_frame_grid():
3394+
# The slices will be ordered and plotted counter-clockwise.
3395+
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
3396+
sizes = [15, 30, 45, 10]
3397+
colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral']
3398+
# only "explode" the 2nd slice (i.e. 'Hogs')
3399+
explode = (0, 0.1, 0, 0)
3400+
3401+
3402+
plt.pie(sizes, explode=explode, labels=labels, colors=colors,
3403+
autopct='%1.1f%%', shadow=True, startangle=90,
3404+
wedgeprops={'linewidth': 0},
3405+
frame=True, center=(2,2))
3406+
3407+
plt.pie(sizes[::-1], explode=explode, labels=labels, colors=colors,
3408+
autopct='%1.1f%%', shadow=True, startangle=90,
3409+
wedgeprops={'linewidth': 0},
3410+
frame=True, center=(5,2))
3411+
3412+
plt.pie(sizes, explode=explode[::-1], labels=labels, colors=colors,
3413+
autopct='%1.1f%%', shadow=True, startangle=90,
3414+
wedgeprops={'linewidth': 0},
3415+
frame=True, center=(3,5))
3416+
# Set aspect ratio to be equal so that pie is drawn as a circle.
3417+
plt.axis('equal')
3418+
3419+
33733420
@cleanup
33743421
def test_margins():
33753422
# test all ways margins can be called

0 commit comments

Comments
 (0)