配件從這段代碼,我可以用「out.best_fit」,我想現在要做的打印最終契合,是畫出每個峯各個高斯曲線,而不是將它們全部合併成一條曲線。情節單個峯與Python-lmfit
from pylab import *
from lmfit import minimize, Parameters, report_errors
from lmfit.models import GaussianModel, LinearModel, SkewedGaussianModel
from scipy.interpolate import interp1d
from numpy import *
fit_data = interp1d(x_data, y_data)
mod = LinearModel()
pars = mod.make_params(slope=0.0, intercept=0.0)
pars['slope'].set(vary=False)
pars['intercept'].set(vary=False)
x_peak = [278.35, 334.6, 375]
y_peak = [fit_data(x) for x in x_peak]
i = 0
for x,y in zip(x_peak, y_peak):
sigma = 1.0
A = y*sqrt(2.0*pi)*sigma
prefix = 'g' + str(i) + '_'
peak = GaussianModel(prefix=prefix)
pars.update(peak.make_params(center=x, sigma=1.0, amplitude=A))
pars[prefix+'center'].set(min=x-20.0, max=x+20.0)
pars[prefix+'amplitude'].set(min=0.0)
mod = mod + peak
i += 1
out = mod.fit(y_data, pars, x=x_data)
plt.figure(1)
plt.plot(x_data, y_data)
plt.figure(1)
plt.plot(x_data, out.best_fit, '--')
情節全球契合:
什麼是'x_data'和'y_data'? – Cleb
對不起,它們只是x和y數據的兩個列表。 – TMR
難道你不能從'out'得到擬合參數嗎?它們必須存儲在某個地方以計算擬合的y值。然後,如果您設法提取擬合參數,則可以繪製單個高斯分佈。 –