|
3 | 3 | Pie Demo2
|
4 | 4 | =========
|
5 | 5 |
|
6 |
| -Make a pie charts using :meth:`.Axes.pie`. |
| 6 | +Make a pie charts of varying size - see |
| 7 | +https://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.pie for the |
| 8 | +docstring. |
7 | 9 |
|
8 |
| -This exampledemonstrates somepiechart features likelabels, varying size, |
9 |
| -autolabeling the percentage, offsetting a sliceand adding a shadow. |
10 |
| -""" |
| 10 | +This exampleshows a basicpiecharts withlabels optional features, |
| 11 | +likeautolabeling the percentage, offsetting a slicewith "explode" |
| 12 | +and adding a shadow, in different sizes. |
11 | 13 |
|
| 14 | +""" |
12 | 15 | importmatplotlib.pyplotasplt
|
| 16 | +frommatplotlib.gridspecimportGridSpec |
13 | 17 |
|
14 | 18 | # Some data
|
| 19 | + |
15 | 20 | labels='Frogs','Hogs','Dogs','Logs'
|
16 | 21 | fracs= [15,30,45,10]
|
17 | 22 |
|
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') |
| 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 | +fortintexts: |
| 45 | +t.set_size('smaller') |
| 46 | +fortinautotexts: |
| 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 | +fortintexts: |
| 57 | +t.set_size('smaller') |
| 58 | +fortinautotexts: |
| 59 | +t.set_size('x-small') |
| 60 | +autotexts[0].set_color('y') |
45 | 61 |
|
46 | 62 | plt.show()
|