matplotlib绘制二层圆环图
时间: 2023-07-04 07:23:18 浏览: 50
可以使用matplotlib中的pie函数来绘制二层圆环图。具体步骤如下:
1. 导入matplotlib库和numpy库:
```python
import matplotlib.pyplot as plt
import numpy as np
```
2. 准备数据:
```python
data_outer = [30, 20, 50] # 外层数据
data_inner = [10, 10, 30, 10, 30] # 内层数据
```
3. 绘制图形:
```python
# 设置颜色
colors_outer = ['#ff9999', '#66b3ff', '#99ff99']
colors_inner = ['#ffcc99', '#99ffff', '#ccffcc', '#ffff99', '#ff9999']
# 绘制外层圆环
fig, ax = plt.subplots()
ax.axis('equal')
outer, _ = ax.pie(data_outer, radius=1.5, labels=['A', 'B', 'C'], labeldistance=0.7, colors=colors_outer, wedgeprops=dict(width=0.3, edgecolor='w'))
# 绘制内层圆环
inner, _ = ax.pie(data_inner, radius=1.2, labels=['a', 'b', 'c', 'd', 'e'], labeldistance=0.5, colors=colors_inner, wedgeprops=dict(width=0.3, edgecolor='w'))
# 添加图例
plt.legend(inner, ['a', 'b', 'c', 'd', 'e'], title='Inner Data', loc='lower right')
# 显示图形
plt.show()
```
运行以上代码,即可得到一个二层圆环图。其中,radius参数表示圆环的半径大小,wedgeprops参数表示圆环的宽度和边框颜色。另外,labeldistance参数表示标签距离圆心的距离。