Skip to content

Commit 191d385

Browse files
committed
Update Pie Demo2
1 parent 2d41a8d commit 191d385

File tree

1 file changed

+27
-42
lines changed

1 file changed

+27
-42
lines changed

examples/pie_and_polar_charts/pie_demo2.py

Lines changed: 27 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,60 +3,45 @@
33
Pie Demo2
44
=========
55
6-
Make a pie charts of varying size - see
7-
https://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.pie for the
8-
docstring.
6+
Make a pie charts using :meth:`.Axes.pie`.
97
10-
This example shows a basic pie charts with labels optional features,
11-
like autolabeling the percentage, offsetting a slice with "explode"
12-
and adding a shadow, in different sizes.
8+
This example shows some pie chart features like labels, varying size,
9+
autolabeling the percentage, offsetting a slice with *explode* and
10+
adding a shadow.
1311
1412
"""
1513
import matplotlib.pyplot as plt
16-
from matplotlib.gridspec import GridSpec
1714

1815
# Some data
19-
2016
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
2117
fracs = [15, 30, 45, 10]
2218

23-
explode = (0, 0.05, 0, 0)
24-
25-
# Make square figures and axes
26-
27-
the_grid = GridSpec(2, 2)
28-
29-
plt.subplot(the_grid[0, 0], aspect=1)
19+
# Make figure and axes
20+
fig, axs = plt.subplots(2, 2)
3021

31-
plt.pie(fracs, labels=labels, autopct='%1.1f%%', shadow=True)
22+
# A standard pie plot
23+
axs[0, 0].pie(fracs, labels=labels, autopct='%1.1f%%', shadow=True)
3224

33-
plt.subplot(the_grid[0, 1], aspect=1)
25+
# Shift the second slice using explode
26+
axs[0, 1].pie(fracs, labels=labels, autopct='%.0f%%', shadow=True,
27+
explode=(0, 0.1, 0, 0))
3428

35-
plt.pie(fracs, explode=explode, labels=labels, autopct='%.0f%%', shadow=True)
29+
# Adapt radius and text size for a smaller pie
30+
patches, texts, autotexts = axs[1, 0].pie(fracs, labels=labels,
31+
autopct='%.0f%%',
32+
textprops={'size': 'smaller'},
33+
shadow=True, radius=0.5)
34+
# Make percent texts even smaller
35+
plt.setp(autotexts, size='x-small')
36+
autotexts[0].set_color('white')
3637

37-
plt.subplot(the_grid[1, 0], aspect=1)
38-
39-
patches, texts, autotexts = plt.pie(fracs, labels=labels,
40-
autopct='%.0f%%',
41-
shadow=True, radius=0.5)
42-
43-
# Make the labels on the small plot easier to read.
44-
for t in texts:
45-
t.set_size('smaller')
46-
for t in autotexts:
47-
t.set_size('x-small')
48-
autotexts[0].set_color('y')
49-
50-
plt.subplot(the_grid[1, 1], aspect=1)
51-
52-
# Turn off shadow for tiny plot with exploded slice.
53-
patches, texts, autotexts = plt.pie(fracs, explode=explode,
54-
labels=labels, autopct='%.0f%%',
55-
shadow=False, radius=0.5)
56-
for t in texts:
57-
t.set_size('smaller')
58-
for t in autotexts:
59-
t.set_size('x-small')
60-
autotexts[0].set_color('y')
38+
# Turn off the shadow for tiny plot with exploded slice.
39+
explode = (0, 0.05, 0, 0)
40+
patches, texts, autotexts = axs[1, 1].pie(fracs, explode=explode,
41+
labels=labels, autopct='%.0f%%',
42+
textprops={'size': 'smaller'},
43+
shadow=False, radius=0.5)
44+
plt.setp(autotexts, size='x-small')
45+
autotexts[0].set_color('white')
6146

6247
plt.show()

0 commit comments

Comments
 (0)