【问题标题】:Is there a way in lmfit to only show the curve of the fit?lmfit 有没有办法只显示拟合曲线?
【发布时间】:2019-07-21 20:21:01
【问题描述】:

所以我在 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

【问题讨论】:

    标签: python lmfit


    【解决方案1】:

    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()
    

    【讨论】:

    • 您好,感谢您的帮助。我遵循了您的代码,然后删除了“final_fit.plot_fit()”行。这似乎解决了我的问题。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-04
    • 2021-11-01
    • 2015-06-20
    • 2022-12-04
    • 1970-01-01
    • 2021-02-15
    • 2015-07-15
    相关资源
    最近更新 更多