matplotlib进行绘图——直方图

参考刘顺祥 数据分析1480

分为七个步骤:

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()

图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值