|
16 | 16 | rotated counter-clockwise by 90 degrees, and the frog slice starts on the
|
17 | 17 | positive y-axis.
|
18 | 18 | """
|
19 |
| -import numpy as np |
20 | 19 | import matplotlib.pyplot as plt
|
21 | 20 |
|
22 | 21 | # Pie chart, where the slices will be ordered and plotted counter-clockwise:
|
23 | 22 | labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
|
24 | 23 | sizes = [15, 30, 45, 10]
|
25 | 24 | explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs')
|
26 | 25 |
|
27 |
| -fg1, ax1 = plt.subplots() |
| 26 | +fig1, ax1 = plt.subplots() |
28 | 27 | ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
|
29 | 28 | shadow=True, startangle=90)
|
30 | 29 | ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
|
31 | 30 |
|
32 |
| - |
33 |
| -# Plot four Pie charts in a 2x2 grid: |
34 |
| -pie_data = [np.roll(sizes, i) for i in range(4)] # generate some data |
35 |
| -pie_centerpos = [(0, 0), (0, 1), (1, 0), (1, 1)] # the grid positions |
36 |
| - |
37 |
| -fg2, ax2 = plt.subplots() |
38 |
| -for data, cpos in zip(pie_data, pie_centerpos): |
39 |
| - _, txts = ax2.pie(data, explode=explode, shadow=True, startangle=90, |
40 |
| - radius=0.35, center=cpos, frame=True, labeldistance=.7) |
41 |
| - # Make texts include number and labels: |
42 |
| - for t, l, d in zip(txts, labels, data): |
43 |
| - t.set_text("%s\n %.1f%%" % (l, d)) |
44 |
| - t.set_horizontalalignment("center") |
45 |
| - t.set_fontsize(8) |
46 |
| - |
47 |
| -ax2.set_xticks([0, 1]) |
48 |
| -ax2.set_yticks([0, 1]) |
49 |
| -ax2.set_xticklabels(["Sunny", "Cloudy"]) |
50 |
| -ax2.set_yticklabels(["Dry", "Rainy"]) |
51 |
| -ax2.set_xlim((-0.5, 1.5)) |
52 |
| -ax2.set_ylim((-0.5, 1.5)) |
53 |
| -ax2.set_aspect('equal') # Equal aspect ratio ensures that the pie is a circle. |
54 |
| - |
55 | 31 | plt.show()
|
0 commit comments