研究生研究LIBS了,开始恶补光学知识,老师安排我通过python进行数据单变量定标曲线,主要是用一元线性拟合曲线,同时画误差棒。 #导入函数库 import matplotlib.pylab as plt import numpy as np import pandas as pd import math from sklearn.metrics import mean_squared_error,r2_score #绘制误差棒图 plt.figure(1) #将数据导入 df = pd.read_excel("E:/pycharm/dingbiao/test.xlsx") df_biaozhun = pd.read_excel("E:/pycharm/dingbiao/铝青铜标准含量表.xlsx") #导入最小值、最大值 #df['min_idx']=df.idxmin(axis=1) df['min_val']=df.min(axis=1) df['max_val']=df.max(axis=1) df['avg']=(df['min_val']+df['max_val'])/2 #df['max_idx']=df.idxmax(axis=1) #print(df['avg']) df_result = pd.concat([df, df_biaozhun['Fe']], axis=1, join='outer', ignore_index=False) #print(df_result) obs_min=df_result['min_val'] obs_max=df_result['max_val'] #print(obs_min) #obs_min,obs_max = np.loadtxt('obs_syn_amp_mean.dat', usecols=(8,9), unpack=True) #导入x以及平均值 x=df_biaozhun['Fe'] obs_mean = df['avg'] #设置errorbar的大小 yerr = np.zeros([6,len(obs_mean)]) yerr[0,:] = obs_mean - obs_min yerr[1,:] = obs_max - obs_mean print(yerr) x_ = np.array(x) obs_mean_ = np.array(obs_mean) #绘制errorbar plt.errorbar(x_,obs_mean_,yerr=yerr[0,:],ecolor='k',elinewidth=0.5,marker='s',mfc='orange',\ mec='k',mew=1,ms=10,alpha=1,capsize=6,capthick=3,linestyle="none",label="Observation") # 设置坐标轴及图例显示信息 plt.xlabel(r"concentration $(wt.\%)$", fontsize=15) plt.ylabel(r"intensity $(a.u.)$", fontsize=15) plt.xticks(fontsize=10) plt.yticks(fontsize=10) plt.legend(fontsize=15) # 将图片保存在当前目录 fig = plt.gcf() fig.set_size_inches(12, 10) fig.savefig('fe.png', dpi=500) #plt.close() #准备数据 p=df_result #读取20行数据 #p1=p.head(20) y=obs_mean_ # 平均值 x_mean = np.mean(x_) y_mean = np.mean(y) #x(或y)列的总数(即n) xsize = x.size zi=((x-x_mean)*(y-y_mean)).sum() mu=((x-x_mean)*(x-x_mean)).sum() n=((y-y_mean)*(y-y_mean)).sum() # 参数a b a = zi / mu b = y_mean - a * x_mean #相关系数R的平方 m=((zi/math.sqrt(mu*n))**2) # 这里对参数保留4位有效数字 a = np.around(a,decimals=4) b = np.around(b,decimals=4) m = np.around(m,decimals=4) print(f'回归线方程:y = {a}x +({b})') print(f'R方为{m}') #借助第三方库skleran画出拟合曲线 y1 = a*x + b plt.scatter(x,y) #plt.plot(x,y1,c='r') plt.plot(x, y, c = 'm', lw = 3., label = 'sin(x)') #s=str(m) #plt.text(6, 0, s,va='baseline') fig = plt.gcf() fig.set_size_inches(12, 10) fig.savefig('fe2.png', dpi=500)
01-29
3191
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-03
1万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
01-26
8400
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
04-17
3285
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)