import matplotlib.pyplot as plt #导入matplotlib库
plt.rcParams['font.sans-serif'] = ['SimHei'] # 设置中文字体为黑体
plt.rcParams['axes.unicode_minus'] = False # 解决负号显示问题
#定义食材种类和食材重量
kinds = ['面粉', '全麦粉', '酵母', '苹果酱', '鸡蛋', '黄油', '盐', '白糖']
weight = [250, 150, 4, 250, 50, 30, 4, 20]
#定义食材种类和食材重量
# 绘制饼图
plt.pie(weight, labels=kinds, autopct='%3.1f%%')
# 设置标题
plt.title('食材配比') # 设置标题
# 将图例放在饼图的右侧,分为四列
plt.legend(loc='upper center', bbox_to_anchor=(1.6, 0.6), ncol=4)
# 创建表格
table_data = [weight]
row_labels = ['重量(g)']
col_labels = kinds
#循环遍历每个单元格,并设置其背景色为粉色
colors = [['pink' for _ in range(len(kinds))] for _ in range(len(row_labels))]
table = plt.table(cellText=table_data, cellLoc='center', rowLabels
python绘制饼图
于 2023-10-20 00:22:44 首次发布