Search code examples
pythongaussianlmfitgaussian-process

How to plot the Gaussian fit for multiple experimental data using lmfit Python


I am trying to plot a Gaussian fit for my experimental data. My data is in the form of a csv file containing two columns of x and y valuesSample csv file. Currently I have 3 csv files in the folder. I am using a 'for' loop to read the csv files in the folder and storing the x and y values in an array. I then, print the arrays to check if all the data values are stored in the array. I am trying to plot a Gaussian fit for my data values. I have attached the snippet of the Gaussian fit I am getting. I want to find a way to plot a fit for each of the csv files. Right now I am getting just 1 fit for all the 3 files as shown in the snippet. I want to generate 3 such fits. Please let me know if you require additional details. I am happy to provide. Any suggestions would really help. Output for the code I have attached my code below:

import csv
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
from scipy.optimize import curve_fit
from scipy import asarray as ar,exp
import lmfit
from glob import glob 
import os
from lmfit import Parameters, minimize, report_fit, Model
xData=np.array(xData)
yData=np.array(yData)
mean=sum(xData*yData)/sum(yData)
sigma=np.sqrt(sum(yData*(xData-mean)**2)/sum(yData))
def Gauss(x,I0,x0,sigma,Background):
    return I0*exp((-(x-x0)**2)/(2*sigma**2))+Background
mod=Model(Gauss)
import glob
path=r'E:\Users\ConfocalUser\Documents\GitHub\qudi\NV_Points'
#path=r"E:\Users\ConfocalUser\Documents\GitHub\qudi"
files=glob.glob(os.path.join(path,'*.csv')) # add the second closing parenthesis here
xData=[]
yData=[]
for file in files:  
    print(file)
    with open(file,"r") as f_in: ##### open 'file' 
        reader=csv.reader(f_in)
        next(reader)  ####NB only use this if CSV has a header line
        for line in reader:
            try:
                float_1,float_2=float(line[0]),float(line[1])
                xData.append(float_1)
                yData.append(float_2)
            except ValueError:
                continue

#############################################################        
#gives fit and results
#result#gives statistics
print('xData:', xData) # show the results
print('yData:', yData) 
###check if the path is correct
Sample output:
E:\Users\ConfocalUser\Documents\GitHub\qudi\NV_Points\0951 x 30.csv
E:\Users\ConfocalUser\Documents\GitHub\qudi\NV_Points\0951 y 30.csv
E:\Users\ConfocalUser\Documents\GitHub\qudi\NV_Points\0951 z 30.csv
xData: [-2.010019542e-06, -1.943019542e-06, -1.876019542e-06, -1.809019542e-06, -1.742019542e-06, etc]
yData: [73313.0, 4769.0, 0.0, 7259.0, 9436.0, 13502.0, 15917.0, 22537.0, 29154.0, 38734.0 etc]
plt.plot(xData,yData,'bo',label='experimental_data')
plt.grid()
plt.show()
result=mod.fit(yData,x=xData,I0=1,x0=mean,sigma=sigma,Background=0)
result.plot()
plt.grid()

Solution

  • import csv
    import pandas as pd
    import numpy as np
    from matplotlib import pyplot as plt
    from scipy.optimize import curve_fit
    from scipy import asarray as ar,exp
    import glob
    import os
    from lmfit import Model
    path=r'E:\Users\ConfocalUser\Documents\GitHub\qudi\NV_Points'
    ##create a function to iterate over a folder and###########################################
    ###########################################################################################
    def getcsvfile():
        xData=[]
        yData=[]
        files=glob.glob(os.path.join(path,'*.csv'))
        for file in files:
            
            with open(file,"r") as f_in: 
                reader=csv.reader(f_in)
                next(reader)  
                for line in reader:
                    try:
                        float_1,float_2=float(line[0]),float(line[1])
                        xData.append(float_1)
                        yData.append(float_2)
    #                     print('x-values for file', file,xData)
    #                     print('y-values for file', file,yData)
                    except ValueError:
                        continue
    ######################### define the function you want to fit (Gaussian-fit)################################
    ############################################################################################################
            xData=np.array(xData)
            yData=np.array(yData)
            mean=sum(xData*yData)/sum(yData)
            sigma=np.sqrt(sum(yData*(xData-mean)**2)/sum(yData))
            def Gauss(x,I0,x0,sigma,Background):
                return I0*exp((-(x-x0)**2)/(2*sigma**2))+Background
    ##################################### Plot the fit for each of the files ###################################
    ############################################################################################################
            mod=Model(Gauss)
            result=mod.fit(yData,x=xData,I0=1,x0=mean,sigma=sigma,Background=0)
            result.plot()
            plt.grid()
            plt.xlabel('x,y,z distribution')
            plt.ylabel('PL intensity')
            basename=os.path.basename(file)
            plt.title(basename)
            print('The fit statistics for',basename)
            print(result.fit_report())#min_correl=0.25))
            xData=[]
            yData=[]
    getcsvfile()