|
3 | 3 | Pie Demo2
|
4 | 4 | =========
|
5 | 5 |
|
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`. |
13 | 7 |
|
| 8 | +This example demonstrates some pie chart features like labels, varying size, |
| 9 | +autolabeling the percentage, offsetting a slice and adding a shadow. |
14 | 10 | """
|
| 11 | + |
15 | 12 | import matplotlib.pyplot as plt
|
16 |
| -from matplotlib.gridspec import GridSpec |
17 | 13 |
|
18 | 14 | # Some data
|
19 |
| - |
20 | 15 | labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
|
21 | 16 | fracs = [15, 30, 45, 10]
|
22 | 17 |
|
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') |
61 | 45 |
|
62 | 46 | plt.show()
|
0 commit comments