统计区间内的样本数画直方图

本文介绍了如何利用Python的matplotlib.pyplot模块(plt.hist)和seaborn库(sns.displot)来绘制直方图,展示统计区间内的样本数量。通过这两种方法,可以清晰地呈现数据分布,并且sns.displot还能自动拟合概率密度函数曲线。

统计区间内的样本数画直方图

一、使用plt.hist

import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
from scipy.stats import norm

# 产生数据
mu = 100  # mean of distribution
sigma = 15  # standard deviation of distribution
x = mu + sigma * np.random.randn(10000)

num_bins = 50 # 区间个数
n, bins, patches = plt.hist(x, num_bins, density=1, facecolor='blue', alpha=0.5) # alpha 透明度 
y = norm.pdf(bins, mu, sigma) # 拟合高斯分布的曲线
plt.plot(bins, y, 'r--')
plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title(r'Histogram of IQ: $\mu=100$, $\sigma=15$')
plt.show()

结果如下图
在这里插入图片描述
也可以使用这种方法拟合y

y = ((1 / (np.sqrt(2 * np.pi) * sigma)) *
     np.exp(-0.5 * (1 / sigma * (bins - mu)) ** 2))

结果:
在这里插入图片描述

二、使用sns.displot

这里可以自动拟合概率密度函数曲线

plt.figure()
sns.distplot(x,bins=50, kde_kws={"color":"red", "lw":2 }, hist_kws={ "color": "blue" },label='LGG4')
plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title('Histogram')
plt.legend()
plt.show()

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值