เพื่อพลอต Nested Pie Chart (Pie Chart ซ้อนกัน) ใน Matplotlib
- ตั้งค่าขนาดรูปและ padding ระหว่าง subplots
- สร้าง figure และ subplots
- กำหนดตัวแปร size สร้าง vals, cmap, outer_colors, inner_colors ด้วย numpy
- ใช้ฟังก์ชัน pie() สร้าง pie chart
- แสดงรูปด้วย show()
ตัวอย่าง
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
fig, ax = plt.subplots()
size = 0.3
vals = np.array([[60., 32.], [37., 40.], [29., 10.]])
cmap = plt.get_cmap("tab20c")
outer_colors = cmap(np.arange(3)*4)
inner_colors = cmap([1, 2, 5, 6, 9, 10])
ax.pie(vals.sum(axis=1), radius=1, colors=outer_colors,
wedgeprops=dict(width=size, edgecolor='w'))
ax.pie(vals.flatten(), radius=1-size, colors=inner_colors,
wedgeprops=dict(width=size, edgecolor='w'))
plt.show()ผลลัพธ์
