问题描述
我正在使用 for 循环遍历多个文件并在 Python 中绘制它的 x-y 值。但是,我的代码在新行中绘制每个图。我有兴趣在移动到下一行之前在一行中绘制 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 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',yData)
except ValueError:
continue
################################plot the xData and yData for each of the files within the folder#############
#############################################################################################################
# plt.plot(xData,yData,'bo',label='experimental_data')
# plt.xlabel('x,y,z distribution')
# plt.ylabel('PL intensity')
# basename = os.path.basename(file)
# plt.title(basename)
# plt.show()
######################### 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=1)
result.plot()
plt.grid()
plt.xlabel('x,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=[]
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)