Python - lmfit没有正确地构建我的数据部分

时间:2018-02-04 19:23:11

标签: python lmfit

我试图通过lmfit来填充我的部分数据(看起来像高斯),但我得到了一条线。请参见下图。

数据绘图:

enter image description here

我的代码我尝试的内容如下:

import matplotlib.pyplot as plt
from numpy import sqrt, pi, exp, linspace, loadtxt
from lmfit import Model


def gaussian(x, amp, cen, wid):
    "1-d gaussian: gaussian(x, amp, cen, wid)"
    return (amp/(sqrt(2*pi)*wid)) * exp(-(x-cen)**2 /(2*wid**2))

gmodel = Model(gaussian)
result = gmodel.fit(n[83:93], x=bins[84:94], amp=2, cen=5, wid=3)

print result.fit_report()

ax = plt.subplot(212)
ax.set_yscale("log", nonposx='clip')


plt.plot(bins[1:len(bins)], n, 'r*')
plt.plot(bins[84:94], result.init_fit, 'r')

plt.grid()
plt.ylabel("Counts")
plt.xlabel("Peak Voltage [V]")
plt.show()

关注我使用

的数据系列

array([ 0.381058  ,  0.41177682,  0.44249564,  0.47321446,  0.50393328,
    0.5346521 ,  0.56537092,  0.59608974,  0.62680856,  0.65752738,
    0.6882462 ,  0.71896502,  0.74968384,  0.78040266,  0.81112148,
    0.8418403 ,  0.87255912,  0.90327794,  0.93399676,  0.96471558,
    0.9954344 ,  1.02615322,  1.05687204,  1.08759086,  1.11830968,
    1.1490285 ,  1.17974732,  1.21046614,  1.24118496,  1.27190378,
    1.3026226 ,  1.33334142,  1.36406024,  1.39477906,  1.42549788,
    1.4562167 ,  1.48693552,  1.51765434,  1.54837316,  1.57909198,
    1.6098108 ,  1.64052962,  1.67124844,  1.70196726,  1.73268608,
    1.7634049 ,  1.79412372,  1.82484254,  1.85556136,  1.88628018,
    1.916999  ,  1.94771782,  1.97843664,  2.00915546,  2.03987428,
    2.0705931 ,  2.10131192,  2.13203074,  2.16274956,  2.19346838,
    2.2241872 ,  2.25490602,  2.28562484,  2.31634366,  2.34706248,
    2.3777813 ,  2.40850012,  2.43921894,  2.46993776,  2.50065658,
    2.5313754 ,  2.56209422,  2.59281304,  2.62353186,  2.65425068,
    2.6849695 ,  2.71568832,  2.74640714,  2.77712596,  2.80784478,
    2.8385636 ,  2.86928242,  2.90000124,  2.93072006,  2.96143888,
    2.9921577 ,  3.02287652,  3.05359534,  3.08431416,  3.11503298,
    3.1457518 ,  3.17647062,  3.20718944,  3.23790826,  3.26862708,
    3.2993459 ,  3.33006472,  3.36078354,  3.39150236,  3.42222118,
    3.45294   ])

array([  33.,  173.,  178.,  187.,  212.,  196.,  194.,  218.,  213.,
    191.,  189.,  236.,  115.,  196.,  211.,  182.,  163.,  161.,
    125.,  123.,  116.,  133.,  104.,  120.,   68.,  138.,   91.,
     81.,   92.,   76.,   89.,   84.,   96.,   86.,   71.,   69.,
     78.,   48.,   84.,   76.,   75.,   99.,   73.,   64.,   93.,
     67.,   92.,   85.,  101.,   38.,   88.,   65.,   54.,   76.,
     63.,   51.,   78.,   81.,   67.,   50.,   79.,   63.,   24.,
     50.,   68.,   58.,   62.,   72.,   53.,   65.,   42.,   54.,
     60.,   79.,   34.,   58.,   53.,   57.,   73.,  102.,   98.,
    116.,  136.,  147.,  107.,  106.,  124.,   47.,   91.,   52.,
     42.,   16.,    4.,    7.,    7.,    6.,   18.,   44.,  853.,  216.])

我试图改变高斯模型但没有成功。也试过其他图书馆。关于发生了什么的任何想法?

1 个答案:

答案 0 :(得分:2)

我不熟悉lmfit包,但是从documentation我认为你不应该在你的情节中使用result.init_fit,而是result.best_fit

来自文档:

result.init_fit numpy.ndarray模型函数的结果,在提供的自变量和初始参数下进行评估。

result.best_fit numpy.ndarray模型函数的结果,在提供的自变量和最佳拟合参数下进行评估。