我试图为我的实验数据绘制一个高斯拟合图。我的数据是csv文件的形式,包含两列x和y值
. 目前我在文件夹中有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()
暂无答案!
目前还没有任何答案,快来回答吧!