简体   繁体   中英

Need Help Python Fitting Gaussian to data that doesn't lie along x-axis at y=0

I have searched a decent amount and have yet to find a clear solution to my question. I currently can fit a Gaussian to any data that lies along the x-axis, the typical set of data you see when looking a Gaussian fitting tutorial. Now I have data that is raised a certain amount above the x-axis so i can't have my Gaussian fit hit the x-axis. My solution is to just define a Gaussian function with an additional + y0 constant. I just don't know how to code this in! Currently I have the following.

n = len(xcut)                            
mean = center                           
sigma = sum(ycut*(xcut-mean)**2)/n        

def gaus(x,a,x0,sigma):
     return a*exp(-(x-x0)**2/(2*sigma**2))

popt,pcov = curve_fit(gaus,xcut,ycut,p0=[45,mean,sigma])

Gauss Fit

I would like to make is so the function is:

def gaus(x,a,x0,sigma,y0):
     return y0+a*exp(-(x-x0)**2/(2*sigma**2))

But after this how do I alter the parameter guesses and such? Similar to amplitude would I input my guess inside p0?

Edit: I can set my y0 to a constant value that I can guess and the fit works very well. But this would require me to give a good guess each time for each data set. Which is a work around, but a pain!

Any help would be great thanks!

p0 is the vector of initial guesses for the parameters. So just append another number to it.

popt,pcov = curve_fit(gaus,xcut,ycut,p0=[45,mean,sigma,initial_y0_guess]

Regarding your question edit on the initial guess for the new y0 parameter, the minimum x value should work. Try passing min(x).

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