You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# 4.1.5:饼图
## 饼图
饼图又称圆饼图、圆形图等,它是利用圆形及圆内扇形面积来表示数值大小的图形。饼图主要用于总体中各组成部分所占比重的研究。
## 绘制饼图
`matplotlib`用来绘制饼图的函数是`pie()`。常用参数如下:
|参数 |作用 |
| :------------: | :------------: |
| x |(每一块的比例如果sum(x) > 1会使用sum(x)归一化|
|labels | 饼图外侧显示的说明文字 |
|explode |(每一块)离开中心距离|
|startangle |起始绘制角度,默认图是从x轴正方向逆时针画起,如设定=90则从y轴正方向画起|
|shadow |在饼图下面画一个阴影。默认值False即不画阴影|
|autopct |控制饼图内百分比设置|
|radius |控制饼图半径默认值为1|
|wedgeprops|字典类型可选参数默认值None。参数字典传递给wedge对象用来画一个饼图。|
```python
labels = 'A','B','C','D'
sizes = [10,20,10,60]
plt.pie(sizes,labels=labels,explode = (0,0.1,0,0),autopct='%1.1f')
plt.show()
```
![](18.jpg)
## 嵌套饼图
嵌套饼图通常被称为空心饼图图表。空心饼图形状的效果是通过`wedgeprops`参数设置馅饼楔形的宽度来实现的:
```python
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(np.array([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'))
ax.set(aspect="equal" )
```
![](19.jpg)