简体   繁体   中英

Plot a model with multiple curve_fit parameters

I have a model that describes a sum of Gaussians distributions:

s1 = np.random.normal(2, 0.5, size = (1000, 1))
s2 = np.random.normal(5, 0.5, size = (1000, 1))


mb = (np.concatenate((s1, s2), axis=0)).max()
Xi = np.arange(0,mb,0.1) #bins
 #histogram population 1
Y11, bins1 = np.histogram(s1, X)
Y1 = Y11/Y11.sum()
X1 = bins1[:-1]

 #histogram population 2
Y22, bins2 = np.histogram(s2, X)
Y2 = Y22/Y22.sum()
X2 = bins2[:-1]

 #universe, with all mixed populations
S =  np.concatenate((s1, s2), axis=0)
Yi, bins = np.histogram(S, Xi)
Y = Yi/Yi.sum()
X = bins[:-1]


def gaussians(X, amp1, mean1, SD1, amp2, mean2, SD2):
    
    A = amp1 * np.exp(-0.5*((X - mean1)/SD1)**2)
    B = amp2 * np.exp(-0.5*((X - mean2)/SD2)**2)
      
    return A + B

params, pcov = curve_fit(gaussians, X,Y, p0=(1,2,1,1,5,1), maxfev=4000)


j = numpy.arange(0.1, mb, 0.1)

plt.figure(figsize=(10, 6))  #size of graph
plt.plot(X, Y, 'o', linewidth=2)
plt.plot(X, gaussians(X ,params[0], params[1],params[2], params[3], params[4], params[5]),'b', linewidth=2) 
plt.xlim([-.01, mb])
plt.ylim([0, 0.1])

plt.show()

This code plot a nice graph as follows: 在此处输入图像描述

I wonder how to plot each gaussian overlapped in the same graph from the parameters of my model function. I mean, something like this (made by hand): 在此处输入图像描述

For those worried to get the answer, I figured out how to do it. It's only matters to become zero all the parameters that you don't want to graph:

plt.plot(X, gaussians(X ,params[0], params[1],params[2], params[3], params[4], params[5]),'b', linewidth=8, alpha=0.1) 
plt.plot(X, gaussians(X ,0, params[1],params[2], params[3], params[4], params[5]),'r', linewidth=1 ) 
plt.plot(X, gaussians(X ,params[0], params[1],params[2], 0, params[4], params[5]),'g', linewidth=1) 
plt.xlim([-.01, mb])

在此处输入图像描述 plt.ylim([0, 0.1])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM