python 正态分布图_Python工具(正态分布对比图)

#coding:utf8

import matplotlib.pyplot as plt

import numpy as np

def visNormal(myArr,deleteOutlier=True,lineColor="k",histColor="#696969",nameOfData="original data",partNum=5):

'''数据的正态分布对比图:==============================================================input:myArr: Inputted array输入数组deleteOutlier: Whether to delete outliers (default:True) 是否删除离群值(默认删除) (mean+/-3*sigma)lineColor: The color of standard normal distribution (default:"k") 标准正态分布(折线图)颜色(默认黑色)histColor: The color of true distribution of original data (default:"k") 原数据分布(柱状图)颜色(默认黑色)partNum: The number of the hists (default:5) 分段数(默认5)imgPath: The path used to save the visualization image (default:"normImg.jpg") 图像存储路径(默认 本地/normImg.jpg)==============================================================return:plt'''

if deleteOutlier == True:#delete outliers 删除离群值

tempMean=np.mean(myArr)

tempStd=np.std(myArr)

myArr=myArr[myArr!=tempMean+3*tempStd]

myArr=myArr[myArr!=tempMean-3*tempStd]

realMax=np.max(myArr)#get the maximum 获取最大值

realMin=np.min(myArr)#get the minimum 获取最小值

partItemScale=(realMax-realMin)/partNum#get the unit distance 获取间隔单位距离

myArrCoor=np.array([[(realMin+i*partItemScale+realMin+(i+1)*partItemScale)/2,myArr[np.logical_and.reduce([myArr>=realMin+i*partItemScale,myArr

pltBar=plt.bar(x=myArrCoor[:,0],height=myArrCoor[:,1],width=(realMax-realMin)/partNum,color=histColor,edgecolor='w')#plot 输出柱状图

myMean=np.mean(myArr)#get the mean of original data 获取原数据均值

myStd=np.std(myArr)#get the standard deviation of original data 获取原数据标准差

normArr=np.random.normal(loc=myMean,scale=myStd,size=myArr.size)#get the data of normal distribution (ND)获取正态分布随机样本

ndMin=np.min(normArr)#get the minimum of ND 获取随机样本最小值

ndMax=np.max(normArr)#get the maxmum of ND 获取随机样本最大值

ndStd=np.std(normArr)#get the standard deviation of ND 获取随机样本标准差

ndPartScale=(ndMax-ndMin)/partNum#get the unit distance of ND 获取随机样本拐点间隔距离

ndCoor=np.array([[(ndMin+i*ndPartScale+ndMin+(i+1)*ndPartScale)/2,normArr[np.logical_and.reduce([normArr>=ndMin+i*ndPartScale,normArr

pltLine,=plt.plot(ndCoor[:,0],ndCoor[:,1],c=lineColor,linestyle="--")#plot 输出折线图

plt.legend((pltBar, pltLine), (nameOfData, 'Normal Data'))#set legend 设定标注

return plt

if __name__=="__main__":

a=np.random.normal(loc=3000,scale=50,size=5000)

myPlt=visNormal(a)

plt.show()

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值