import numpy as np
import matplotlib.pyplot as plt
from scipy import optimize
data = np.genfromtxt('NORMAL.txt')
def gaussian(x, height, center, width, offset):
return height*np.exp(-(x - center)**2/(2*width**2)) + offset
def six_gaussians(x, h1, c1, w1, h2, c2, w2, h3, c3, w3, h4, c4, w4, h5, c5,
w5, h6, c6, w6, h7, c7, w7, h8, c8, w8, offset):
return (gaussian(x, h1, c1, w1, offset=0) +
gaussian(x, h2, c2, w2, offset=0) +
gaussian(x, h3, c3, w3, offset=0) +
gaussian(x, h4, c4, w4, offset=0) +
gaussian(x, h5, c5, w5, offset=0) +
gaussian(x, h6, c6, w6, offset=0) +
gaussian(x, h6, c6, w6, offset=0) +
gaussian(x, h6, c6, w6, offset=0) + offset)
errfunc6 = lambda p, x, y: (six_gaussians(x, *p) - y)**2
guess6 = [28.7, 4.3, 0.01, 27.6, 3.5, 0.01, 53.3, 1.0, 0.5, 28.4, -2.67, 0.5,
44.3, -7.32, 0.5, 34.8, -13.5, 0.1, 20, -11, 10, 23, 3.3, 1, 0] # I guess
there are 8 peaks, 6 are clear, but between them there seems to be another
one, based on the change in slope smoothness there
optim3, success = optimize.leastsq(errfunc6, guess6[:], args=(data[:,0],
data[:,1]))
optim3
plt.plot(data[:,0], data[:,1], lw=5, c='g', label='measurement')
plt.gca().invert_xaxis()
plt.plot(data[:,0], six_gaussians(data[:,0], *optim3),
lw=3, c='b', label='fit of 6 Gaussians')
plt.legend(loc='best')
plt.savefig('result_fitting.png')
这是我的高斯拟合代码。 我得到了数据。你知道吗
您只需调用
plt.figure
:这个调用创建了一个新的图形用于打印,所以现在需要保存两个图形,其中一个应该在创建新图形之前保存。你知道吗
相关问题 更多 >
编程相关推荐