import matplotlib.pyplot as plt
# 准备数据
labels = ['A', 'B', 'C', 'D']
sizes = [30, 45, 15, 10]
# 绘制饼图
plt.pie(sizes, labels=labels, autopct='%.1f%%')
# 显示图形
plt.show()
运行结果
解释
1. `import matplotlib.pyplot as plt`:这一行导入了 `matplotlib.pyplot` 模块,通常简称为 `plt`。这个模块提供了常用的绘图功能,包括绘制饼图。
2. `labels = ['A', 'B', 'C', 'D']`:这个列表用于指定每个扇形的标签,也就是每个扇形所代表的内容或类别。在这个例子中,我们有四个标签:'A'、'B'、'C' 和 'D'。
3. `sizes = [30, 45, 15, 10]`:这个列表用于指定每个扇形的大小,也就是每个类别在整体中所占的比例。在这个例子中,我们有四个大小:30、45、15 和 10。
4. `plt.pie(s