[英]How to plot the Gaussian fit for multiple experimental data using lmfit Python
我正在嘗試繪制適合我的實驗數據的高斯圖。 我的數據是包含兩列 x 和 y 值的 csv 文件的形式 . 目前我在文件夾中有 3 個 csv 文件。 我正在使用“for”循環讀取文件夾中的 csv 文件並將 x 和 y 值存儲在數組中。 然后,打印數組以檢查所有數據值是否都存儲在數組中。 我正在嘗試繪制適合我的數據值的高斯圖。 我附上了我得到的高斯擬合的片段。 我想找到一種為每個 csv 文件繪制擬合的方法。 現在我只得到 1 個適合所有 3 個文件,如代碼段所示。 我想生成 3 個這樣的擬合。 如果您需要其他詳細信息,請告訴我。 我很樂意提供。 任何建議都會真正有幫助。
我在下面附上了我的代碼:
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()
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.