用matplotlib,画一个双层饼图
时间: 2023-07-04 14:07:43 浏览: 111
好的,以下是一个简单的例子:
```python
import matplotlib.pyplot as plt
# 数据
sizes_outer = [30, 70]
sizes_inner = [20, 40, 10, 30]
# 颜色
colors_outer = ['red', 'lightgrey']
colors_inner = ['blue', 'green', 'yellow', 'purple']
# 标签
labels_outer = ['Group A', 'Group B']
labels_inner = ['Category 1', 'Category 2', 'Category 3', 'Category 4']
# 创建子图
fig, ax = plt.subplots()
# 添加外层饼图
wedges_outer, _ = ax.pie(sizes_outer, wedgeprops=dict(width=0.5), startangle=-40, colors=colors_outer)
# 添加内层饼图
wedges_inner, _ = ax.pie(sizes_inner, wedgeprops=dict(width=0.5), startangle=-90, colors=colors_inner, radius=0.7)
# 添加标签
ax.legend(wedges_outer, labels_outer, loc="center", bbox_to_anchor=(0.5, -0.15))
ax.legend(wedges_inner, labels_inner, loc="center right", bbox_to_anchor=(1, 0, 0.3, 1))
# 设置标题
ax.set_title("Double-layer Pie Chart")
# 显示图形
plt.show()
```
该例子会输出一个双层饼图,其中外层饼图有两个扇形(30% 和 70%),内层饼图有四个扇形(20%、40%、10% 和 30%)。外层饼图和内层饼图的颜色和标签都是不同的。如下图所示:

阅读全文