Skip to content

Commit 6b4a3b1

Browse files
update nested-pie example with donut
1 parent adc0b93 commit 6b4a3b1

File tree

1 file changed

+35
-23
lines changed

1 file changed

+35
-23
lines changed

examples/pie_and_polar_charts/nested_pie.py

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
=================
55
66
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.
88
99
"""
1010

@@ -17,17 +17,29 @@
1717
#
1818
# In this case, pie takes values corresponding to counts in a group.
1919
# 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
2222
# 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+
2326

24-
vals = np.array([[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6]])
2527
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`')
3028

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`')
3143
plt.show()
3244

3345
###############################################################################
@@ -40,24 +52,24 @@
4052

4153
fig, ax = plt.subplots(subplot_kw=dict(polar=True))
4254

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)
4661

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]))
5165

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")
5669

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")
6173

6274
ax.set(title="Pie plot with `ax.bar` and polar coordinates")
6375
ax.set_axis_off()

0 commit comments

Comments
 (0)