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!

dciampa
  • 73
  • 1
  • 6

2 Answers2

0

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]

Victor Chubukov
  • 1,345
  • 1
  • 10
  • 18
0

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

James Phillips
  • 4,526
  • 3
  • 13
  • 11