import matplotlib.pyplot as plt
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 = [['blue' for _ in range(len(kinds))] for _ in range(len(row_labels))]
table = plt.table(cellText=table_data, cellLoc='center', rowLabels=row_labels, colLabels=col_labels, loc='bottom right',cellColours=colors, colWidths=[0.1] * len(col_labels))
# 设置表格单元格的背景色为蓝色
for cell in table.get_celld().values():
cell.set_facecolor('blue')
table.auto_set_font_size(False) # 关闭自动调整字体大小
table.set_fontsize(9) # 设置表格字体大小
table.scale(1, 2) # 调整表格的整体缩放比例,使表格高度增加一倍
plt.show()