【python】生成正态分布样本并画出PDF和CDF图

该篇文章介绍了如何使用Python中的numpy、scipy和matplotlib库来生成正态分布的随机样本,计算频率分布、概率密度函数(PDF)以及累积分布函数(CDF),并通过图表展示结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

"""
@author: zhang
"""
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt

mean,std=1,0.3
sample_size=2000

# norm distribution
samples=stats.norm.rvs(mean,std,size=sample_size)

res=stats.relfreq(samples,numbins=20)
pdf_value=res.frequency
cdf_value=np.cumsum(res.frequency)

x=res.lowerlimit+np.linspace(0,res.binsize*res.frequency.size,res.frequency.size)

fig,(ax1,ax2)=plt.subplots(2,1,figsize=(5,6))
ax1.set_xlabel('Frequency (MHz)')
ax1.set_ylabel('Probability')
ax1.hist(samples,15,density=True)

ax1.set_xlim([x.min(),x.max()])
ax1.grid(True,linestyle='--',alpha=0.4)
ax2.set_xlabel('Frequency (MHz)')
ax2.set_ylabel('Cumulative Probability')
ax2.plot(x,cdf_value)
ax2.grid(True,linestyle='--',alpha=0.4)

plt.show()

运行后结果如下:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值