lmfit 中有没有办法只显示拟合曲线?
Is there a way in lmfit to only show the curve of the fit?
所以我在 lmfit 的帮助下编写了一些代码,以在一些直方图数据上拟合高斯曲线。虽然曲线本身很好,但当我尝试在 matplotlib 中绘制结果时,它会显示拟合以及数据点。实际上,我想用曲线拟合绘制直方图条。你怎么做到这一点?或者,在 lmfit 中有没有办法只显示拟合曲线,然后添加直方图并将它们组合在一起?
我的代码的相关部分:
counts, bin_edges = np.histogram(some_array, bins=1000)
bin_widths = np.diff(bin_edges)
x = bin_edges[:-1] + (bin_widths / 2)
y = counts
mod = GaussianModel()
pars = mod.guess(y, x=x)
final_fit = mod.fit(y, pars, x=x)
final_fit.plot_fit()
plt.show()
这是图表结果:
Gaussian curve
lmfit 的内置绘图例程是 matplotlib 的最小包装器,旨在为许多情况提供合理的默认绘图。他们不制作直方图。
但是数组很容易获得,使用 matplotlib 制作直方图也很容易。我想你只需要:
import matplotlib.pyplot as plt
plt.hist(some_array, bins=1000, rwidth=0.5, label='binned data')
plt.plot(x, final_fit.best_fit, label='best fit')
plt.legend()
plt.show()
所以我在 lmfit 的帮助下编写了一些代码,以在一些直方图数据上拟合高斯曲线。虽然曲线本身很好,但当我尝试在 matplotlib 中绘制结果时,它会显示拟合以及数据点。实际上,我想用曲线拟合绘制直方图条。你怎么做到这一点?或者,在 lmfit 中有没有办法只显示拟合曲线,然后添加直方图并将它们组合在一起?
我的代码的相关部分:
counts, bin_edges = np.histogram(some_array, bins=1000)
bin_widths = np.diff(bin_edges)
x = bin_edges[:-1] + (bin_widths / 2)
y = counts
mod = GaussianModel()
pars = mod.guess(y, x=x)
final_fit = mod.fit(y, pars, x=x)
final_fit.plot_fit()
plt.show()
这是图表结果: Gaussian curve
lmfit 的内置绘图例程是 matplotlib 的最小包装器,旨在为许多情况提供合理的默认绘图。他们不制作直方图。
但是数组很容易获得,使用 matplotlib 制作直方图也很容易。我想你只需要:
import matplotlib.pyplot as plt
plt.hist(some_array, bins=1000, rwidth=0.5, label='binned data')
plt.plot(x, final_fit.best_fit, label='best fit')
plt.legend()
plt.show()