问题描述
我希望看到标准分布(或累积百分比)以及正态分布中的值,类似这样。
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
vals = np.random.randint(0,10,1000)
vals.sort()
mean = np.mean(vals)
sd = np.std(vals)
pdf = stats.norm.pdf(vals,mean,sd)
fig = plt.figure(figsize=(6,5))
ax = fig.add_subplot(111)
ax.plot(vals,pdf,c='blue',marker='.')
ax.set_xlabel("vals",fontsize=10)
ax.set_ylabel("p",fontsize=10)
ax.grid(b=True,which='major',color='0.55',linestyle='--')
ax.grid(b=True,which='minor',color='0.85',linestyle='--')
ax.minorticks_on()
plt.show()
解决方法
你可以这样做:
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
vals = np.random.randint(0,10,1000)
vals.sort()
na = np.array(vals,dtype='float')
mean = np.mean(vals)
sd = np.std(vals)
pdf = stats.norm.pdf(vals,mean,sd)
fl = True
l = []
l.append(mean)
co = 1
while(fl):
left = mean-(co*sd)
l.insert(0,left)
right = mean+(co*sd)
l.insert(len(l),right)
if ((left<=vals[0]) | (right>=vals[-1])):
fl = False
co = co +1;
fig = plt.figure(figsize=(6,5))
ax = fig.add_subplot(111)
ax.plot(vals,pdf,c='blue',marker='.')
ax.set_xlabel("vals",fontsize=10)
ax.set_ylabel("p",fontsize=10)
ax.grid(b=True,which='major',color='0.55',linestyle='--')
ax.grid(b=True,which='minor',color='0.85',linestyle='--')
ax.minorticks_on()
g = lambda x: stats.norm.cdf(x,loc=mean,scale=sd)
for a,b in list(zip(l[1:],l)):
plt.axvline(a,color='red',label='z=ε')
plt.axvline(b,label='z=ε')
plt.fill_between(vals,where=((b<=na) | (na<=a)),color='gray')
plt.text(a-sd,0.05,"{:.2f}".format((g(a)-g(b))),fontsize=12,rotation=45)
plt.show()
可以根据您的要求随意编辑代码。
,感谢@Pygirl 对 Z 分数的初步想法,这符合我的目的。话虽如此,非常感谢您用累积百分比修改脚本的努力,干杯!
以下是对我有用的方法。
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
np.random.seed(42)
vals = np.random.randint(0,1000)
vals.sort()
mean = np.mean(vals)
sd = np.std(vals)
pdf = stats.norm.pdf(vals,sd)
zScore = stats.zscore(vals)
fig = plt.figure(figsize=(6,c='b',color='0.5',linestyle='--')
ax2 = ax.twiny()
ax2.plot(zScore,marker='.')
ax2.grid(b=True,color='r',linestyle='-')
ax2.set_xlabel("zScore",fontsize=10)
plt.show()