Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I am trying to plot a Gaussian fit for my data using lmfit Python. Right now, I have a folder containing 3 csv files. Each of the csv files have 2 columns (x-values and y-values). I am storing the contents of the x and y data in two arrays. I want to loop through the folder and plot the x-y graph and also it's Gaussian fit.
Right now, I am able to see only one plot for the 3 csv files I have.
I want to see 'n' plots for 'n' files along with their file names. I have attached my core below. Any suggestions would really help!.
My sample csv file is
x0000	y0000
-7.66E-06	17763
-7.60E-06	2853
-7.53E-06	3694
-7.46E-06	4203


What I have tried:

import csv
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' not 'path
        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)
result=mod.fit(yData,x=xData,I0=1,x0=mean,sigma=sigma,Background=0)
result.plot()
plt.grid()
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()
###check if the path is correct
Posted
Updated 11-Jul-21 6:59am
Comments
Richard MacCutchan 11-Jul-21 4:53am    
Interesting that you have retained the comments in my correction of your original code.

As to the problem, you are reading the data from all files into your arrays before you calculate the plot. I suggest you rework your code to make use of a couple of functions. The first one should read the next file in sequence and return the two arrays. the second should take the two arrays and plot the values. That way you can display the filename in front of each plot.

1 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()
 
Share this answer
 
Comments
CHill60 13-Jul-21 11:58am    
A code dump with no commentary nor explanation does not make a good solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900