分为七个步骤:
1、导入模块
2、设置绘图风格
3、导入数据
4、设置图框的大小
5、绘图
6、添加轴标签和标题
7、显示图形
# 导入第三方包
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
# 设置设置绘图风格
# print (plt.style.available)
plt.style.use("Solarize_Light2")
plt.rcParams['font.sans-serif']= 'SimHei'
# 读取wechart数据集
df = pd.read_excel(r'C:\Users\guanyang\Desktop\wechart.xlsx')
print(df.head())
# 设置图框的大小
fig = plt.figure(num =1, figsize=(10, 6), facecolor = 'y')
# 绘图:阅读章节数的频数直方图
plt.hist(df.article_reading_cnts, # 绘图数据
bins = np.arange(df.article_reading_cnts.min(),df.article_reading_cnts.max(),30), # 指定直方图的组距
density = True, # 设置为频率直方图
color = 'steelblue', # 指定填充色
edgecolor = 'g',
label="阅读章节数的频数直方图") # 指定直方图的边界色
# 绘制绘制正态分布曲线
x1 = np.linspace(df.article_reading_cnts.min(),df.article_reading_cnts.max(),20)
normal = mlab.normpdf(x1, x1.mean(), x1.std())
plt.plot(x1, normal, 'r', linewidth = 2, label="正态分布曲线")
# 添加轴标签和标题
plt.title('阅读章节数统计图')
plt.xlabel('阅读章节数')
plt.ylabel('天数')
# 显示图形
fig.autofmt_xdate(rotation = 45)
plt.legend(title="图例", loc="best")
plt.show()
图: