1.绘制饼图
(1)导入模块
import matplotlib.pyplot as plt
import numpy as np
(2) 设置中文
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
(3)数据准备
kinds = ['面粉', '全麦粉', '酵母', '苹果酱', '鸡蛋', '黄油', '盐', '白糖']
weight = [250, 150, 4, 250, 50, 30, 4, 20]
(4)绘制饼图
plt.pie(weight, autopct='%3.1f%%')
(5)添加辅助元素
#添加图例
plt.legend(kinds, loc='upper right', bbox_to_anchor=[2.10, 0.75], ncol=4)
#添加表格
colors = [['orange', 'orange', 'orange', 'orange','orange', 'orange', 'orange',
'orange']]
plt.table(cellText=[weight], cellLoc='center', colLabels=kinds,
rowLabels=['重量(g)'], loc='bottom', rowColours=['grey'],
cellColours=colors, bbox=[1.15, 0.25, 0.9, 0.25],
colColours['grey','grey','grey','grey','grey','grey','grey','grey'])
(6)展示图表
plt.show()