python - 将条形图转变为正态分布

标签 python scipy seaborn

我是 python 新手。

我有 2 个数组和一个漂亮的条形图:

# Buyers in %
h =[1,1,3,5,9,13,16,16,14,10,5,4,2,1,0]

# Clothes size
x =  [34,35,36,37,38,39,40,41,42,43,44,45,46,47,48]

# P(X=40) =  16 % // The probability that some buyers gets a 40 sized clothe is 16 %
# P(37 <= X <= 40)  = 5+9+13+16 = 43 % // The probability that somes buyers gets between 37 and 40 sized clothes is 43 %

plt.ylabel('Buyers % ')
plt.xlabel('Clothes Size')
plt.bar(x, height = h)
plt.grid(True)
plt.show()

enter image description here

  1. 我如何使用seaborn或scipy.stats.norm将其转换为密度线和正态分布并将其绘制在条形上?
  2. 之后,我如何使用正态分布计算 P(X<40) ?

谢谢。

最佳答案

使用seaborn:

# Buyers in %
h =[1,1,3,5,9,13,16,16,14,10,5,4,2,1,0]

# Clothes size
x =  [34,35,36,37,38,39,40,41,42,43,44,45,46,47,48]
import seaborn as sns
from scipy.stats import norm
data = []
for i in range(len(x)): data += [x[i]]*h[i] 
sns.set()
plt.figure(figsize=(10,5),dpi=300)
sns.distplot(data, fit=norm, kde=False)

resulting plot

获取概率:

from scipy.stats import norm
import numpy as np
sample = data
sample_mean = np.array(data).mean()
sample_std = np.array(data).std()
min_value = int(sample_mean-4*sample_std)
max_value = int(sample_mean+4*sample_std)
dist = norm(sample_mean, sample_std)
values = [value for value in range(min_value, max_value)]
probabilities = [dist.pdf(value) for value in values]

#plt.plot(values,probabilities)

def prob(min_lim,max_lim):
    p = (np.array(values)>min_lim).astype(int)* (np.array(values)<max_lim).astype(int)
    prob = (np.array(probabilities)[p.astype(bool)]).sum()
    return prob

prob(0,40)

Out[2]: 0.3230891372830226

注意:它与计算的不同,因为它使用根据数据的平均值和标准差估计的连续正态分布。

如果你不想使用连续估计,代码就是:

len(np.array(data)[np.array(data)<40])/len(data)
Out[2]: 0.32

关于python - 将条形图转变为正态分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59582648/

相关文章:

python - 使用 F2PY 的可调节 Fortran 数组

python - 如何在数组中将列彼此串联

python - scipy odeint 中的当前迭代

python - NumPy 的/科学的 : How to re-construct an ndarray?

python - subplot2grid 与 seaborn 覆盖相同的斧头

python - 如何为 Seaborn Facet Plot 添加标题

python - 如何在python中绘制一条线,每个数据点都有一个间隔

Python加速重建列表的代码

list - Python:将项目添加到列表直到条件为真

python - Scipy:收敛指标