我希望法线曲线适合我已经拥有的直方图。 navf2是归一化随机数的列表,直方图基于这些随机数,我想要一条曲线来显示直方图的总体趋势。
while len(navf2)<252:
number=np.random.normal(0,1,None)
navf2.append(number)
bin_edges=np.arange(70,130,1)
plt.style.use(["dark_background",'ggplot'])
plt.hist(navf2, bins=bin_edges, alpha=1)
plt.ylabel("Frequency of final NAV")
plt.xlabel("Ranges")
ymin=0
ymax=100
plt.ylim([ymin,ymax])
plt.show()
答案 0 :(得分:0)
您在这里:
= ^ .. ^ =
from scipy.stats import norm
import numpy as np
import matplotlib.pyplot as plt
# create raw data
data = np.random.uniform(size=252)
# distribution fitting
mu, sigma = norm.fit(data)
# fitting distribution
x = np.linspace(-0.5,1.5,100)
y = norm.pdf(x, loc=mu, scale=sigma)
# plot data
plt.plot(x, y,'r-')
plt.hist(data, density=1, alpha=1)
plt.show()
输出:
答案 1 :(得分:0)
这是使用问题中提到的代码的另一种解决方案。我们无需使用scipy库就可以达到预期的结果。我们将要做三件事,计算数据集的平均值,计算数据集的标准偏差,并创建一个生成法线或高斯曲线的函数。
要计算均值,我们可以在numpy库中使用该函数,即mu = np.mean(your_data_set_here)
集合的标准偏差是值和均方https://en.wikipedia.org/wiki/Standard_deviation的差之和的平方根。我们可以再次使用numpy库以如下代码表示它:
data_set = [] # some data set
sigma = np.sqrt(1/(len(data_set))*sum((data_set-mu)**2))
最后,我们必须为正态曲线或高斯https://en.wikipedia.org/wiki/Gaussian_function构建函数,它依赖于均值(mu
)和标准偏差(sigma
),因此我们将在我们的函数中将它们用作参数:
def Gaussian(x,sigma,mu): # sigma is the standard deviation and mu is the mean
return ((1/(np.sqrt(2*np.pi)*sigma))*np.exp(-(x-mu)**2/(2*sigma**2)))
将它们放在一起看起来像这样:
import numpy as np
import matplotlib.pyplot as plt
navf2 = []
while len(navf2)<252:
number=np.random.normal(0,1,None) # since all values will be between 0,1 the bin size doesnt work
navf2.append(number)
navf2 = np.asarray(navf2) # convert to array for better results
mu = np.mean(navf2) #the avg of all values in navf2
sigma = np.sqrt(1/(len(navf2))*sum((navf2-mu)**2)) # standard deviation of navf2
x_vals = np.arange(min(navf2),max(navf2),0.001) # create a flat range based off data
# to build the curve
gauss = [] #store values for normal curve here
def Gaussian(x,sigma,mu): # defining the normal curve
return ((1/(np.sqrt(2*np.pi)*sigma))*np.exp(-(x-mu)**2/(2*sigma**2)))
for val in x_vals :
gauss.append(Gaussian(val,sigma,mu))
plt.style.use(["dark_background",'ggplot'])
plt.hist(navf2, density = 1, alpha=1) # add density = 1 to fix the scaling issues
plt.ylabel("Frequency of final NAV")
plt.xlabel("Ranges")
plt.plot(x_vals,gauss)
plt.show()
这是输出的图片:
希望这会有所帮助,我累了让它尽可能接近您的原始代码!