matplotlib绘制饼图
时间: 2023-05-13 17:06:35 浏览: 62
可以使用以下代码绘制饼图:
```
import matplotlib.pyplot as plt
labels = ['A', 'B', 'C', 'D']
sizes = [15, 30, 45, 10]
colors = ['red', 'green', 'blue', 'yellow']
plt.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%', startangle=90)
plt.axis('equal')
plt.show()
```
其中,labels 是每个扇形的标签,sizes 是每个扇形的大小,colors 是每个扇形的颜色。autopct='%1.1f%%' 表示显示每个扇形的百分比,startangle=90 表示从哪个角度开始绘制饼图。最后使用 plt.show() 显示饼图。
相关问题
用Matplotlib绘制饼图
好的,下面是用Matplotlib绘制饼图的代码示例:
```python
import matplotlib.pyplot as plt
# 数据
labels = ['A', 'B', 'C', 'D'] # 标签
sizes = [15, 30, 45, 10] # 数据列表
colors = ['red', 'green', 'blue', 'yellow'] # 颜色列表
# 画饼图
plt.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%', startangle=90)
# 添加标题
plt.title('Pie Chart')
# 显示图表
plt.show()
```
希望这个代码示例能够帮助你绘制出你想要的饼图。
Matplotlib 绘制饼图介绍
Matplotlib 是一个 Python 中的绘图库,可以用于生成各种类型的图表。其中,绘制饼图也是 Matplotlib 中的一项功能。
下面是绘制饼图的基本步骤:
1. 导入 Matplotlib 库和 NumPy 库(如果需要使用 NumPy)。
``` python
import matplotlib.pyplot as plt
import numpy as np
```
2. 准备数据。饼图需要一个列表来表示每个部分的大小,以及一个列表来表示每个部分的标签。
``` python
sizes = [40, 30, 20, 10]
labels = ['A', 'B', 'C', 'D']
```
3. 绘制饼图。可以使用 `plt.pie()` 函数绘制饼图。该函数的参数包括大小列表、标签列表、起始角度、阴影、文本属性等。
``` python
plt.pie(sizes, labels=labels, startangle=90, shadow=True)
```
4. 显示图表。使用 `plt.show()` 函数显示生成的饼图。
``` python
plt.show()
```
下面是一个完整的绘制饼图的示例代码:
``` python
import matplotlib.pyplot as plt
sizes = [40, 30, 20, 10]
labels = ['A', 'B', 'C', 'D']
plt.pie(sizes, labels=labels, startangle=90, shadow=True)
plt.show()
```
这会生成一个简单的饼图,其中每个部分的大小和标签由 `sizes` 和 `labels` 列表指定。可以根据需要修改这些值来生成不同的饼图。