写一个python画饼图的代码并附上标题
时间: 2024-02-05 13:05:18 浏览: 65
好的,以下是使用matplotlib库绘制饼图的Python程序,并添加标题:
```python
import matplotlib.pyplot as plt
# 饼图数据
labels = ['Apple', 'Banana', 'Orange', 'Grape']
sizes = [30, 25, 20, 25]
colors = ['red', 'yellow', 'orange', 'purple']
explode = [0.1, 0, 0, 0]
# 绘制饼图
fig1, ax1 = plt.subplots()
ax1.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', startangle=90)
ax1.axis('equal')
# 添加标题
ax1.set_title('Fruit Distribution')
# 显示图像
plt.show()
```
说明:
1. 首先定义饼图的数据,包括标签、大小、颜色和分裂程度;
2. 然后使用matplotlib库绘制饼图,其中autopct='%1.1f%%'表示显示百分数,startangle=90表示从90度开始绘制;
3. 最后使用set_title()方法添加饼图的标题。
希望这个程序能够帮到你,如果你还有其他问题或者需要进一步的帮助,可以随时提出。
阅读全文
相关推荐






