目录
1、饼状图
python
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
#from matplotlib.ticker import MaxNLocator
# 解决中文乱码
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['font.sans-serif']='Microsoft YaHei' # 设置中文字体为微软雅黑
# 加载excel
files="C:\\Users\\24453\\Desktop\\py 表格.xlsx"
data=pd.read_excel(files)
# 设置画布大小
plt.figure(figsize=(10,5))
x=data['摄取平均值 g/天'] # 加入数据
labels=data['类别'] # 设定标签
explode=[0.1,0.1,0.1,0.1,0.1] # 设置饼图突出程度
plt.title("肉类摄入占比",fontsize=15)
plt.pie(x,labels=labels,autopct='%3.1f%%',labeldistance=1.08,startangle=100,textprops={'fontsize':12},
explode=explode)
# 图例
plt.legend(data['类别'],loc=(1,0.8))
# 表格
plt.table(cellText=[data['类别'],data['摄取平均值 g/天']], rowLabels=['','摄取平均值 g/天'],colLabels=None,loc='bottom')
plt.show()
2、条形统计图
python
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
#from matplotlib.ticker import MaxNLocator
# 解决中文乱码
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['font.sans-serif']='Microsoft YaHei' # 设置中文字体为微软雅黑
# 加载excel
files="C:\\Users\\24453\\Desktop\\每日食物.xlsx"
data=pd.read_excel(files)
data.plot.bar(x="类别",y="数量",color='orange',title="每周每日统计图")
# y轴刻度设置
plt.yticks(range(0,120000,20000),["%d"%x for x in range(0,120000,20000)],size=10)
# 标签显现 紧凑型布局
plt.tight_layout()
# x轴字体旋转角度
ax=plt.gca()
ax.set_xticklabels(data['类别'],rotation=45,ha='right')
# 表格
#plt.table(cellText=[data['类别'],data['数量']], rowLabels=['','数量'],colLabels=None,loc='top')
plt.show()