# 如何使用scipy.stats.norm.cdf和matplotlib获得Sigmodal CDF曲线？

``````import numpy as np
from numpy.random import default_rng
from scipy.stats import norm
import matplotlib.pyplot as plt

siz = 1000
rg = default_rng( 12345 )
a = rg.random(size=siz)
rg = default_rng( 12345 )
b = norm.rvs(size=siz, random_state=rg)
c = norm.cdf(b)

print( 'a = ', a)
print( 'b = ', b)
print( 'c = ', c)

fig, ax = plt.subplots(3, 1)
acount, abins, aignored = ax[0].hist( a, bins=20, histtype='bar', label='a', color='C0' )
bcount, bbins, bignored = ax[1].hist( b, bins=20, histtype='bar', label='b', color='C1' )
ccount, cbins, cignored = ax[2].hist( c, bins=20, histtype='bar', label='c', color='C2' )
print( 'acount, abins, aignored = ', acount, abins, aignored)
print( 'bcount, bbins, bignored = ', bcount, bbins, bignored)
print( 'ccount, cbins, cignored = ', ccount, cbins, cignored)
ax[0].legend()
ax[1].legend()
ax[2].legend()
plt.show()
``````

• iut 回复

现在我不知道您的特定应用程序。但是我认为问题在于您正在为许多正态分布的随机数创建cdf的值。 在下面，您可以看到一个代码示例，该示例绘制了从-3到+3的标准法线的CDF

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

x = np.arange(-3, 3, 0.1)
c = norm.cdf(x)

plt.plot(x, c)
plt.show()
``````

CDF of standard normal

• uid 回复

You are plotting the wrong values. when you do `b = norm.rvs(size=siz, random_state=rg)`, what you get is a 10 independently drawn random samples from standard normal distribution, i.e., `z` values

因此，它们的直方图就是您看到的钟形曲线。

`norm.cdf` returns the `cfd` value at a given z value. If you want cdf's S curve, you can draw uniformly from -3 to 3 z values and get their cdf values at all points . then you plot the output probability values.

编辑：另一个答案给出了这种方法的代码，所以我不会再麻烦添加。