|
4 | 4 | =================
|
5 | 5 |
|
6 | 6 | The following examples show two ways to build a nested pie chart
|
7 |
| -in Matplotlib. |
| 7 | +in Matplotlib. Such charts are often referred to as donut charts. |
8 | 8 |
|
9 | 9 | """
|
10 | 10 |
|
|
17 | 17 | #
|
18 | 18 | # In this case, pie takes values corresponding to counts in a group.
|
19 | 19 | # We'll first generate some fake data, corresponding to three groups.
|
20 |
| -# In the outer circle, we'll treat each number as belonging to its |
21 |
| -# own group. In the inner circle, we'll plot them as members of their |
| 20 | +# In the inner circle, we'll treat each number as belonging to its |
| 21 | +# own group. In the outer circle, we'll plot them as members of their |
22 | 22 | # original 3 groups.
|
| 23 | +# The effect of the donut shape is achieved by setting a `width` to |
| 24 | +# the pie's wedges through the `wedgeprops` argument. |
| 25 | + |
23 | 26 |
|
24 |
| -vals = np.array([[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6]]) |
25 | 27 | fig, ax = plt.subplots()
|
26 |
| -ax.pie(vals.flatten(), radius=1.2, |
27 |
| - colors=plt.rcParams["axes.prop_cycle"].by_key()["color"][:vals.shape[1]]) |
28 |
| -ax.pie(vals.sum(axis=1), radius=1) |
29 |
| -ax.set(aspect="equal", title='Pie plot with `ax.pie`') |
30 | 28 |
|
| 29 | +size = 0.3 |
| 30 | +vals = np.array([[60., 32.], [37., 40.], [29., 10.]]) |
| 31 | + |
| 32 | +cm = plt.get_cmap("tab20c") |
| 33 | +cout = cm(np.arange(3)*4) |
| 34 | +cin = cm(np.array([1,2,5,6,9,10])) |
| 35 | + |
| 36 | +ax.pie(vals.sum(axis=1), radius=1, colors=cout, |
| 37 | + wedgeprops=dict(width=size, edgecolor='w')) |
| 38 | + |
| 39 | +ax.pie(vals.flatten(), radius=1-size, colors=cin, |
| 40 | + wedgeprops=dict(width=size, edgecolor='w')) |
| 41 | + |
| 42 | +ax.set(aspect="equal", title='Pie plot with `ax.pie`') |
31 | 43 | plt.show()
|
32 | 44 |
|
33 | 45 | ###############################################################################
|
|
40 | 52 |
|
41 | 53 | fig, ax = plt.subplots(subplot_kw=dict(polar=True))
|
42 | 54 |
|
43 |
| -left_inner = np.arange(0.0, 2 * np.pi, 2 * np.pi / 6) |
44 |
| -left_middle = np.arange(0.0, 2 * np.pi, 2 * np.pi / 12) |
45 |
| -left_outer = np.arange(0.0, 2 * np.pi, 2 * np.pi / 9) |
| 55 | +size = 0.3 |
| 56 | +vals = np.array([[60., 32.], [37., 40.], [29., 10.]]) |
| 57 | +#normalize vals to 2 pi |
| 58 | +valsnorm = vals/np.sum(vals)*2*np.pi |
| 59 | +#obtain the ordinates of the bar edges |
| 60 | +valsleft = np.cumsum(np.append(0,valsnorm.flatten()[:-1])).reshape(vals.shape) |
46 | 61 |
|
47 |
| -ax.bar(x=left_inner, |
48 |
| - width=2 * np.pi / 6, bottom=0, color='C0', |
49 |
| - linewidth=2, edgecolor='w', |
50 |
| - height=np.zeros_like(left_inner) + 5) |
| 62 | +cm = plt.get_cmap("tab20c") |
| 63 | +cout = cm(np.arange(3)*4) |
| 64 | +cin = cm(np.array([1,2,5,6,9,10])) |
51 | 65 |
|
52 |
| -ax.bar(x=left_middle, |
53 |
| - width=2 * np.pi / 12, bottom=5, color='C1', |
54 |
| - linewidth=2, edgecolor='w', |
55 |
| - height=np.zeros_like(left_middle) + 2) |
| 66 | +ax.bar(x=valsleft[:,0], |
| 67 | + width=valsnorm.sum(axis=1), bottom=1-size, height=size, |
| 68 | + color=cout, edgecolor='w', linewidth=1, align="edge") |
56 | 69 |
|
57 |
| -ax.bar(x=left_outer, |
58 |
| - width=2 * np.pi / 9, bottom=7, color='C2', |
59 |
| - linewidth=2, edgecolor='w', |
60 |
| - height=np.zeros_like(left_outer) + 3) |
| 70 | +ax.bar(x=valsleft.flatten(), |
| 71 | + width=valsnorm.flatten(), bottom=1-2*size, height=size, |
| 72 | + color=cin, edgecolor='w', linewidth=1, align="edge") |
61 | 73 |
|
62 | 74 | ax.set(title="Pie plot with `ax.bar` and polar coordinates")
|
63 | 75 | ax.set_axis_off()
|
|
0 commit comments