如何解决直方图与高斯函数的拟合似乎是错误的
如何用高斯函数拟合简单的直方图?这是错误的,不是吗?为什么曲线不宽?
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
def gaussian(x,mean,amplitude,standard_deviation):
return amplitude * np.exp( - ((x - mean) / standard_deviation) ** 2)
x = [4.1,4.5,4.6,4.9,5,4.7,5.2,4.95,5.65,5.05]
fig,ax = plt.subplots(figsize=(5,5))
bin_heights,bin_borders,_ = plt.hist(x,bins='auto',label='histogram')
bin_centers = bin_borders[:-1] + np.diff(bin_borders) / 2
popt,_ = curve_fit(gaussian,bin_centers,bin_heights,p0=[1.,0.,1.])
x_interval_for_fit = np.linspace(bin_borders[0],bin_borders[-1],10000)
plt.plot(x_interval_for_fit,gaussian(x_interval_for_fit,*popt),label='fit')
plt.legend()
plt.xlim(3,7)
plt.show()
是否可以将拟合延长到比直方图范围更大的长度?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。