Python 基于正态分布的绘图点
Python Plot points based on normal distribution
我想检查一个随机点是否在正态分布内。从我的代码中可以看出,我使用不同的颜色将它们分开;然而,结果是错误的 - 标准是如果一个点在分布的内部或外部然后相应地着色。
import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as stats
mu = 175
sigma = 10
xnormal = np.linspace(-3 * sigma + mu, 3 * sigma + mu, 1000)
xnormal = [int(x) for x in xnormal]
plt.plot(xnormal, stats.norm.pdf(xnormal, mu, sigma))
plt.axhline(y=0.04, color='r', linestyle='-')
N = 100
for i in range(N):
x = np.random.randint(145, 205)
y = np.random.uniform(0.0, 0.04)
if(y <= (xnormal.count(x)/1000)):
plt.scatter(x,y,None,'orange')
else:
plt.scatter(x,y,None,'b')
plt.show()
程序输出的内容:
结果应该是什么:
小索引问题:
import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as stats
#just inserted for reproducibility
np.random.seed(123)
mu = 175
sigma = 10
xnormal = np.linspace(-3 * sigma + mu, 3 * sigma + mu, 1000)
xnormal = [int(x) for x in xnormal]
ynormal = stats.norm.pdf(xnormal, mu, sigma)
plt.plot(xnormal, ynormal)
plt.axhline(y=0.04, color='r', linestyle='-')
N = 100
for i in range(N):
x = np.random.randint(145, 205)
y = np.random.uniform(0.0, 0.04)
if(y <= ynormal[xnormal.index(x)]):
plt.scatter(x,y,None,'orange')
else:
plt.scatter(x,y,None,'b')
plt.show()
示例输出:
我想检查一个随机点是否在正态分布内。从我的代码中可以看出,我使用不同的颜色将它们分开;然而,结果是错误的 - 标准是如果一个点在分布的内部或外部然后相应地着色。
import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as stats
mu = 175
sigma = 10
xnormal = np.linspace(-3 * sigma + mu, 3 * sigma + mu, 1000)
xnormal = [int(x) for x in xnormal]
plt.plot(xnormal, stats.norm.pdf(xnormal, mu, sigma))
plt.axhline(y=0.04, color='r', linestyle='-')
N = 100
for i in range(N):
x = np.random.randint(145, 205)
y = np.random.uniform(0.0, 0.04)
if(y <= (xnormal.count(x)/1000)):
plt.scatter(x,y,None,'orange')
else:
plt.scatter(x,y,None,'b')
plt.show()
程序输出的内容:
结果应该是什么:
小索引问题:
import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as stats
#just inserted for reproducibility
np.random.seed(123)
mu = 175
sigma = 10
xnormal = np.linspace(-3 * sigma + mu, 3 * sigma + mu, 1000)
xnormal = [int(x) for x in xnormal]
ynormal = stats.norm.pdf(xnormal, mu, sigma)
plt.plot(xnormal, ynormal)
plt.axhline(y=0.04, color='r', linestyle='-')
N = 100
for i in range(N):
x = np.random.randint(145, 205)
y = np.random.uniform(0.0, 0.04)
if(y <= ynormal[xnormal.index(x)]):
plt.scatter(x,y,None,'orange')
else:
plt.scatter(x,y,None,'b')
plt.show()
示例输出: