簡體   English   中英

我如何才能從擬合Python中分別畫出高斯?

[英]How can i draw separately the gaussians from fitting in python?

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

plt.savefig('without_gausians.png')    
plt.figure()
...
plt.savefig('result_fitting.png')

此調用將創建一個新圖形以進行繪制,因此現在您需要保存兩個圖形,其中一個應在創建新圖形之前保存。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM