【发布时间】:2017-06-20 23:03:53
【问题描述】:
我正在尝试将高斯拟合到似乎遵循高斯分布的一组数据点。我已经检查了很多可能的方法来做到这一点,但我并不真正了解其中的大多数。但是,我找到了一种似乎可行的解决方案,但我得到的实际拟合看起来并不像我的数据点那样更像高斯。
这是我的代码:
import numpy as np
import matplotlib.pyplot as plt
from scipy import asarray as ar, exp, sqrt
from scipy.optimize import curve_fit
angles = [-8, -6, -4, -2, 0, 2, 4, 6, 8]
data = [99, 610, 1271, 1804, 1823, 1346, 635, 125, 24]
angles = ar(angles)
data = ar(data)
n = len(x)
mean = sum(data*angles)/n
sigma = sqrt(sum(data*(angles-mean)**2)/n)
def gaus(x,a,mu,sigma):
return a*exp(-(x-mu)**2/(2*sigma**2))
popt,pcov = curve_fit(gaus,angles,data,p0=[0.18,mean,sigma])
fig = plt.figure()
plt.plot(angles, data, "ob", label = "Measured")
plt.plot(angles,gaus(angles,*popt),'r',label='Fit')
plt.xlim(-10, 10)
plt.ylim(0, 2000)
plt.xticks(angles)
plt.title("$^{137}$Cs Zero Point")
plt.xlabel("Angle [$^\circ$]")
plt.ylabel("662 keV-Photon Count")
plt.grid()
plt.legend()
plt.show()
这是它生成的输出:
如您所见,拟合并不能描述一个漂亮且对称的“真实”高斯。 有什么办法可以得到“更好”的高斯,或者这是否尽可能好?
非常感谢!
【问题讨论】:
-
n = len(x)可能是n = len(data)?
标签: python matplotlib plot gaussian data-fitting