python 光谱折线图
时间: 2023-10-18 10:30:40 浏览: 104
要绘制光谱折线图,你可以使用Python中的matplotlib库来实现。以下是一个简单的示例代码:
```python
import matplotlib.pyplot as plt
# 光谱数据
wavelength = [400, 450, 500, 550, 600, 650, 700] # 波长
intensity = [0.2, 0.5, 0.8, 1.0, 0.7, 0.4, 0.1] # 强度
# 绘制折线图
plt.plot(wavelength, intensity, 'b-') # 使用蓝色实线
plt.xlabel('Wavelength') # x轴标签
plt.ylabel('Intensity') # y轴标签
plt.title('Spectrum') # 图标题
plt.show() # 显示图形
```
在这个示例中,我们假设有一组波长和相应的强度数据。我们使用`plt.plot()`函数来绘制折线图,并指定使用蓝色实线。然后使用`plt.xlabel()`和`plt.ylabel()`分别设置x轴和y轴的标签,最后使用`plt.title()`设置图标题。最后使用`plt.show()`显示图形。
你可以根据你的光谱数据进行相应的修改和调整,以满足你的需求。
相关问题
使用python编写基于紫外-可见光谱的水质COD检测代码,读取三份excel文件,分别为10mg/L,15mg/L,20mg/L的标准溶液,excel文件中第一列数据为波长,第二列为透过率,对数据进行一阶导数计算,得到一阶导数谱图,并绘制在同一个折线图上,再计算一阶导数谱的排列熵,通过排列熵值进行特征波长的提取,最后,利用提取的特征波长处的特征值与对应的COD浓度进行PLS建模,得到COD的预测模型
好的,这是一个比较复杂的任务,需要用到多个Python库,我会一步一步地为您讲解代码实现过程。在开始之前,请确保已经安装了以下库:
- pandas
- numpy
- matplotlib
- scipy
- scikit-learn
首先,读取三份excel文件,分别为10mg/L,15mg/L,20mg/L的标准溶液。假设这三份文件分别为"10mg.xlsx","15mg.xlsx"和"20mg.xlsx",并且这三个文件都有两列数据,第一列为波长,第二列为透过率。
```python
import pandas as pd
# 读取10mg/L的标准溶液
df1 = pd.read_excel('10mg.xlsx')
wavelength = df1.iloc[:,0] # 波长
transmittance1 = df1.iloc[:,1] # 透过率
# 读取15mg/L的标准溶液
df2 = pd.read_excel('15mg.xlsx')
transmittance2 = df2.iloc[:,1] # 透过率
# 读取20mg/L的标准溶液
df3 = pd.read_excel('20mg.xlsx')
transmittance3 = df3.iloc[:,1] # 透过率
```
接下来,对数据进行一阶导数计算,得到一阶导数谱图,并绘制在同一个折线图上。
```python
import numpy as np
import matplotlib.pyplot as plt
# 计算一阶导数
def derivative(x, y):
dy = np.gradient(y, x)
dx = np.gradient(x)
dydx = dy / dx
return dydx
# 计算一阶导数谱
def derivative_spectrum(wavelength, transmittance):
dydx = derivative(wavelength, transmittance)
dydx_spectrum = np.abs(dydx)
return dydx_spectrum
# 计算三个标准溶液的一阶导数谱
dydx_spectrum1 = derivative_spectrum(wavelength, transmittance1)
dydx_spectrum2 = derivative_spectrum(wavelength, transmittance2)
dydx_spectrum3 = derivative_spectrum(wavelength, transmittance3)
# 绘制三个标准溶液的一阶导数谱图
plt.plot(wavelength, dydx_spectrum1, label='10mg/L')
plt.plot(wavelength, dydx_spectrum2, label='15mg/L')
plt.plot(wavelength, dydx_spectrum3, label='20mg/L')
plt.xlabel('Wavelength (nm)')
plt.ylabel('Derivative spectrum')
plt.legend()
plt.show()
```
下一步是计算一阶导数谱的排列熵,通过排列熵值进行特征波长的提取。
```python
from scipy.stats import entropy
# 计算一阶导数谱的排列熵
def permutation_entropy(spectrum):
p = np.zeros((len(spectrum), len(spectrum)))
for i in range(len(spectrum)):
for j in range(len(spectrum)):
if spectrum[i] <= spectrum[j]:
p[i,j] = 1
pe = entropy(p.sum(axis=1))
return pe
# 计算三个标准溶液的一阶导数谱的排列熵
pe1 = permutation_entropy(dydx_spectrum1)
pe2 = permutation_entropy(dydx_spectrum2)
pe3 = permutation_entropy(dydx_spectrum3)
# 提取特征波长
feature_wavelength = wavelength[np.argmin([pe1, pe2, pe3])]
```
最后,利用提取的特征波长处的特征值与对应的COD浓度进行PLS建模,得到COD的预测模型。假设COD浓度数据保存在"COD.xlsx"文件中,第一列为COD浓度,第二列为特征波长处的特征值。
```python
from sklearn.cross_decomposition import PLSRegression
# 读取COD浓度数据
df_cod = pd.read_excel('COD.xlsx')
cod_concentration = df_cod.iloc[:,0] # COD浓度
feature_value = df_cod.iloc[:,1] # 特征波长处的特征值
# 进行PLS建模
X = np.array(feature_value).reshape(-1, 1)
y = np.array(cod_concentration)
pls = PLSRegression(n_components=1)
pls.fit(X, y)
# 预测COD浓度
predict_value = pls.predict(np.array(feature_wavelength).reshape(1, -1))[0]
print('Predicted COD concentration:', predict_value)
```
完整代码如下:
```python
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import entropy
from sklearn.cross_decomposition import PLSRegression
# 读取10mg/L的标准溶液
df1 = pd.read_excel('10mg.xlsx')
wavelength = df1.iloc[:,0] # 波长
transmittance1 = df1.iloc[:,1] # 透过率
# 读取15mg/L的标准溶液
df2 = pd.read_excel('15mg.xlsx')
transmittance2 = df2.iloc[:,1] # 透过率
# 读取20mg/L的标准溶液
df3 = pd.read_excel('20mg.xlsx')
transmittance3 = df3.iloc[:,1] # 透过率
# 计算一阶导数
def derivative(x, y):
dy = np.gradient(y, x)
dx = np.gradient(x)
dydx = dy / dx
return dydx
# 计算一阶导数谱
def derivative_spectrum(wavelength, transmittance):
dydx = derivative(wavelength, transmittance)
dydx_spectrum = np.abs(dydx)
return dydx_spectrum
# 计算三个标准溶液的一阶导数谱
dydx_spectrum1 = derivative_spectrum(wavelength, transmittance1)
dydx_spectrum2 = derivative_spectrum(wavelength, transmittance2)
dydx_spectrum3 = derivative_spectrum(wavelength, transmittance3)
# 绘制三个标准溶液的一阶导数谱图
plt.plot(wavelength, dydx_spectrum1, label='10mg/L')
plt.plot(wavelength, dydx_spectrum2, label='15mg/L')
plt.plot(wavelength, dydx_spectrum3, label='20mg/L')
plt.xlabel('Wavelength (nm)')
plt.ylabel('Derivative spectrum')
plt.legend()
plt.show()
# 计算一阶导数谱的排列熵
def permutation_entropy(spectrum):
p = np.zeros((len(spectrum), len(spectrum)))
for i in range(len(spectrum)):
for j in range(len(spectrum)):
if spectrum[i] <= spectrum[j]:
p[i,j] = 1
pe = entropy(p.sum(axis=1))
return pe
# 计算三个标准溶液的一阶导数谱的排列熵
pe1 = permutation_entropy(dydx_spectrum1)
pe2 = permutation_entropy(dydx_spectrum2)
pe3 = permutation_entropy(dydx_spectrum3)
# 提取特征波长
feature_wavelength = wavelength[np.argmin([pe1, pe2, pe3])]
# 读取COD浓度数据
df_cod = pd.read_excel('COD.xlsx')
cod_concentration = df_cod.iloc[:,0] # COD浓度
feature_value = df_cod.iloc[:,1] # 特征波长处的特征值
# 进行PLS建模
X = np.array(feature_value).reshape(-1, 1)
y = np.array(cod_concentration)
pls = PLSRegression(n_components=1)
pls.fit(X, y)
# 预测COD浓度
predict_value = pls.predict(np.array(feature_wavelength).reshape(1, -1))[0]
print('Predicted COD concentration:', predict_value)
```
使用python编写基于紫外-可见光谱的水质COD检测代码,读取三份excel文件,分别为10mg/L,15mg/L,20mg/L的标准溶液,excel文件中第一列数据为波长,第二列为透过率,对数据通过导数法进行预处理,得到一阶导数谱图,并绘制在同一个折线图上,再计算一阶导数谱的排列熵,通过排列熵算法进行特征波长的提取,最后,利用提取的特征波长处的特征值与对应的COD浓度进行PLS建模,得到COD的预测模型
首先,需要安装并导入所需的库,包括`pandas`、`numpy`、`matplotlib`、`scipy`和`sklearn`。可以使用以下代码安装和导入这些库:
```python
!pip install pandas numpy matplotlib scipy scikit-learn
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import savgol_filter
from sklearn.cross_decomposition import PLSRegression
```
接下来,读取并处理excel数据,得到一阶导数谱图。可以使用以下代码读取数据并绘制谱图:
```python
# 读取excel数据
data1 = pd.read_excel('10mg_L.xlsx')
data2 = pd.read_excel('15mg_L.xlsx')
data3 = pd.read_excel('20mg_L.xlsx')
# 获取波长和透过率数据
wavelength = data1.iloc[:, 0].values
tr1 = data1.iloc[:, 1].values
tr2 = data2.iloc[:, 1].values
tr3 = data3.iloc[:, 1].values
# 对透过率数据进行预处理,得到一阶导数谱图
def get_derivative_spectrum(tr):
# 对透过率数据进行Savitzky-Golay滤波
tr_smoothed = savgol_filter(tr, 31, 3)
# 计算一阶导数
dy = np.diff(tr_smoothed)
dx = np.diff(wavelength)
dy_dx = dy / dx
# 对一阶导数进行Savitzky-Golay滤波
dy_dx_smoothed = savgol_filter(dy_dx, 31, 3)
return dy_dx_smoothed
ds1 = get_derivative_spectrum(tr1)
ds2 = get_derivative_spectrum(tr2)
ds3 = get_derivative_spectrum(tr3)
# 绘制一阶导数谱图
plt.plot(wavelength[1:], ds1, label='10mg/L')
plt.plot(wavelength[1:], ds2, label='15mg/L')
plt.plot(wavelength[1:], ds3, label='20mg/L')
plt.xlabel('Wavelength (nm)')
plt.ylabel('First Derivative')
plt.legend()
plt.show()
```
接下来,计算一阶导数谱的排列熵,并通过排列熵算法提取特征波长。可以使用以下代码实现:
```python
# 计算一阶导数谱的排列熵
def get_permutation_entropy(ds):
# 对一阶导数谱进行离散化
ds_discrete = np.digitize(ds, np.histogram(ds, bins=20)[1])
# 计算排列熵
n = len(ds_discrete)
pe = 0
for m in range(2, 7):
count = {}
for i in range(n - m + 1):
seg = tuple(ds_discrete[i:i+m])
if seg in count:
count[seg] += 1
else:
count[seg] = 1
pe_m = 0
for seg in count:
p = count[seg] / (n - m + 1)
pe_m -= p * np.log(p)
pe += pe_m
return pe
# 提取特征波长
pe1 = get_permutation_entropy(ds1)
pe2 = get_permutation_entropy(ds2)
pe3 = get_permutation_entropy(ds3)
f1 = wavelength[np.argmin(np.abs(ds1 - pe1))]
f2 = wavelength[np.argmin(np.abs(ds2 - pe2))]
f3 = wavelength[np.argmin(np.abs(ds3 - pe3))]
print('Feature wavelengths:', f1, f2, f3)
```
最后,利用特征波长处的特征值与对应的COD浓度进行PLS建模,得到COD的预测模型。可以使用以下代码实现:
```python
# 读取COD浓度数据
cod1 = 10
cod2 = 15
cod3 = 20
# 构建PLS模型
X_train = np.array([ds1[np.argmin(np.abs(wavelength - f1))], ds2[np.argmin(np.abs(wavelength - f2))], ds3[np.argmin(np.abs(wavelength - f3))]]).reshape(3, 1)
y_train = np.array([cod1, cod2, cod3]).reshape(3, 1)
pls = PLSRegression(n_components=1)
pls.fit(X_train, y_train)
# 预测COD浓度
X_test = np.array([ds1[np.argmin(np.abs(wavelength - f1))], ds2[np.argmin(np.abs(wavelength - f2))], ds3[np.argmin(np.abs(wavelength - f3))]]).reshape(3, 1)
y_pred = pls.predict(X_test)
print('Predicted COD concentrations:', y_pred[:, 0])
```
这样就完成了基于紫外-可见光谱的水质COD检测代码的编写。完整代码如下:
```python
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import savgol_filter
from sklearn.cross_decomposition import PLSRegression
# 读取excel数据
data1 = pd.read_excel('10mg_L.xlsx')
data2 = pd.read_excel('15mg_L.xlsx')
data3 = pd.read_excel('20mg_L.xlsx')
# 获取波长和透过率数据
wavelength = data1.iloc[:, 0].values
tr1 = data1.iloc[:, 1].values
tr2 = data2.iloc[:, 1].values
tr3 = data3.iloc[:, 1].values
# 对透过率数据进行预处理,得到一阶导数谱图
def get_derivative_spectrum(tr):
# 对透过率数据进行Savitzky-Golay滤波
tr_smoothed = savgol_filter(tr, 31, 3)
# 计算一阶导数
dy = np.diff(tr_smoothed)
dx = np.diff(wavelength)
dy_dx = dy / dx
# 对一阶导数进行Savitzky-Golay滤波
dy_dx_smoothed = savgol_filter(dy_dx, 31, 3)
return dy_dx_smoothed
ds1 = get_derivative_spectrum(tr1)
ds2 = get_derivative_spectrum(tr2)
ds3 = get_derivative_spectrum(tr3)
# 绘制一阶导数谱图
plt.plot(wavelength[1:], ds1, label='10mg/L')
plt.plot(wavelength[1:], ds2, label='15mg/L')
plt.plot(wavelength[1:], ds3, label='20mg/L')
plt.xlabel('Wavelength (nm)')
plt.ylabel('First Derivative')
plt.legend()
plt.show()
# 计算一阶导数谱的排列熵
def get_permutation_entropy(ds):
# 对一阶导数谱进行离散化
ds_discrete = np.digitize(ds, np.histogram(ds, bins=20)[1])
# 计算排列熵
n = len(ds_discrete)
pe = 0
for m in range(2, 7):
count = {}
for i in range(n - m + 1):
seg = tuple(ds_discrete[i:i+m])
if seg in count:
count[seg] += 1
else:
count[seg] = 1
pe_m = 0
for seg in count:
p = count[seg] / (n - m + 1)
pe_m -= p * np.log(p)
pe += pe_m
return pe
# 提取特征波长
pe1 = get_permutation_entropy(ds1)
pe2 = get_permutation_entropy(ds2)
pe3 = get_permutation_entropy(ds3)
f1 = wavelength[np.argmin(np.abs(ds1 - pe1))]
f2 = wavelength[np.argmin(np.abs(ds2 - pe2))]
f3 = wavelength[np.argmin(np.abs(ds3 - pe3))]
print('Feature wavelengths:', f1, f2, f3)
# 读取COD浓度数据
cod1 = 10
cod2 = 15
cod3 = 20
# 构建PLS模型
X_train = np.array([ds1[np.argmin(np.abs(wavelength - f1))], ds2[np.argmin(np.abs(wavelength - f2))], ds3[np.argmin(np.abs(wavelength - f3))]]).reshape(3, 1)
y_train = np.array([cod1, cod2, cod3]).reshape(3, 1)
pls = PLSRegression(n_components=1)
pls.fit(X_train, y_train)
# 预测COD浓度
X_test = np.array([ds1[np.argmin(np.abs(wavelength - f1))], ds2[np.argmin(np.abs(wavelength - f2))], ds3[np.argmin(np.abs(wavelength - f3))]]).reshape(3, 1)
y_pred = pls.predict(X_test)
print('Predicted COD concentrations:', y_pred[:, 0])
```
阅读全文