0

I am trying to fit some data with Gaussian fit. This data from lateral flow image. The fitted line (red) does not cover data. Please check my code. In the code x is just index. y actually real data.

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit



y = np.array([2.22097081, 2.24776432, 2.35519896, 2.43780396, 2.49708355,
       2.54224971, 2.58350984, 2.62965057, 2.68644093, 2.75454015,
       2.82912617, 2.90423835, 2.97921199, 3.05864617, 3.14649922,
       3.2430853 , 3.3471892 , 3.45919857, 3.58109399, 3.71275641,
       3.84604379, 3.94884214, 3.94108998, 3.72148453, 3.28407665,
       2.7651018 ])

x = np.linspace(1,np.mean(y),len(y))


n = len(x)
mean = sum(x*y)/n
sigma = np.sqrt(sum(y*(x-mean)**2)/n)

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

popt,pcov = curve_fit(gaus,x,y,p0=[1,mean,sigma])

plt.figure()
plt.plot(x,y,'b+:',label='data')
plt.plot(x,gaus(x,*popt),'ro:',label='fit')
plt.legend()
plt.xlabel('Index')
plt.ylabel('Row Mean')

enter image description here

Abdullah
  • 47
  • 1
  • 8
  • Does this answer your question? [Fit a gaussian function](https://stackoverflow.com/questions/11507028/fit-a-gaussian-function) – Hagbard Nov 04 '19 at 13:05
  • No. it's not working – Abdullah Nov 04 '19 at 13:17
  • Well, it fits your data as good as possible (It minimizes the distance between the gaussian function and your data points). Your data simply cannot be fitter better than that with a gaussian function. You have 26 data points, but only 3 parameters which control the shape of the gaussian function. How do you think this is supposed to work? Do you want it to perfectly hit the data points? – AnsFourtyTwo Nov 04 '19 at 13:44
  • thanks for your comments. is there a way to fit the entire data?? do you have any idea? – Abdullah Nov 04 '19 at 14:12
  • Your data does not represent a gaussian function. If it should, then there is something wrong with the measuring process. If it shouldn't necessarily, then a gaussian function might not be the right model – user8408080 Nov 04 '19 at 21:01

0 Answers0