8000 Merge pull request #11083 from timhoffm/pie-demo2 · matplotlib/matplotlib@8f1a399 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8f1a399

Browse files
authored
Merge pull request #11083 from timhoffm/pie-demo2
Update Pie Demo2
2 parents 64050ca + eb1b776 commit 8f1a399

File tree

1 file changed

+31
-47
lines changed

1 file changed

+31
-47
lines changed

examples/pie_and_polar_charts/pie_demo2.py

Lines changed: 31 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,60 +3,44 @@
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.
9-
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.
6+
Make a pie charts using :meth:`.Axes.pie`.
137
8+
This example demonstrates some pie chart features like labels, varying size,
9+
autolabeling the percentage, offsetting a slice and adding a shadow.
1410
"""
11+
1512
import matplotlib.pyplot as plt
16-
from matplotlib.gridspec import GridSpec
1713

1814
# Some data
19-
2015
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
2116
fracs = [15, 30, 45, 10]
2217

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)
30-
31-
plt.pie(fracs, labels=labels, autopct='%1.1f%%', shadow=True)
32-
33-
plt.subplot(the_grid[0, 1], aspect=1)
34-
35-
plt.pie(fracs, explode=explode, labels=labels, autopct='%.0f%%', shadow=True)
36-
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')
18+
# Make figure and axes
19+
fig, axs = plt.subplots(2, 2)
20+
21+
# A standard pie plot
22+
axs[0, 0].pie(fracs, labels=labels, autopct='%1.1f%%', shadow=True)
23+
24+
# Shift the second slice using explode
25+
axs[0, 1].pie(fracs, labels=labels, autopct='%.0f%%', shadow=True,
26+
explode=(0, 0.1, 0, 0))
27+
28+
# Adapt radius and text size for a smaller pie
29+
patches, texts, autotexts = axs[1, 0].pie(fracs, labels=labels,
30+
autopct='%.0f%%',
31+
textprops={'size': 'smaller'},
32+
shadow=True, radius=0.5)
33+
# Make percent texts even smaller
34+
plt.setp(autotexts, size='x-small')
35+
autotexts[0].set_color('white')
36+
37+
# Use a smaller explode and turn of the shadow for better visibility
38+
patches, texts, autotexts = axs[1, 1].pie(fracs, labels=labels,
39+
autopct='%.0f%%',
40+
textprops={'size': 'smaller'},
41+
shadow=False, radius=0.5,
42+
explode=(0, 0.05, 0, 0))
43+
plt.setp(autotexts, size='x-small')
44+
autotexts[0].set_color('white')
6145

6246
plt.show()

0 commit comments

Comments
 (0)
0