You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求Gnuplot/Python绘制UV/VIS光谱脚本:含delta峰与Gaussian拟合曲线

使用Gnuplot绘制UV/VIS光谱:Delta峰+高斯集合曲线

先把你的光谱数据保存为uv_data.txt文件,内容如下:

Wavelength  Intensity
     360.1  0.00003
     358.2  0.004
     357    0.0001
     355    0.0007
     352.3  0.018
     352    0.000001
     349    0.0001
     348    0.0005
     346    0.0033

Gnuplot脚本示例

可自定义高斯宽度(如下方sigma=1.5),脚本内容:

# 输出配置
set terminal pngcairo enhanced font 'Arial,10' size 800,600
set output 'uv_spectrum.png'

# 绘图样式设置
set title "UV/VIS Spectrum: Delta Peaks + Gaussian Sum"
set xlabel "Wavelength (nm)"
set ylabel "Intensity"
set grid

# 定义高斯函数,sigma为宽度参数
sigma = 1.5

# 绘制delta峰(竖线样式)+ 高斯叠加曲线
plot 'uv_data.txt' using 1:2 with impulses lc rgb 'blue' lw 2 title 'Delta Peaks', \
     '-' using 1:2 with lines lc rgb 'red' lw 2 title 'Gaussian Sum (sigma=1.5)'

# 生成采样x轴并计算高斯总和
x_min = 345
x_max = 361
dx = 0.1
do for [x = x_min: x_max: dx] {
    sum = 0
    stats 'uv_data.txt' nooutput
    do for [i=2:int(STATS_records)] {
        mu = word(stats_string(i), 1) + 0.0
        amp = word(stats_string(i), 2) + 0.0
        sum = sum + amp * exp(-(x - mu)**2/(2*sigma**2))
    }
    print x, sum
}
e

运行脚本后生成uv_spectrum.png,蓝色竖线为delta峰,红色曲线为所有高斯峰叠加的趋势线,修改sigma值即可调整高斯宽度。


使用Python绘制UV/VIS光谱:Delta峰+高斯集合曲线

需提前安装依赖:pip install numpy matplotlib

Python脚本示例

import numpy as np
import matplotlib.pyplot as plt

# 加载光谱数据
data = np.loadtxt('uv_data.txt', skiprows=1)
wavelengths = data[:, 0]
intensities = data[:, 1]

# 定义高斯函数
def gaussian(x, mu, sigma, amp):
    return amp * np.exp(-(x - mu)**2 / (2 * sigma**2))

# 设置高斯宽度
sigma = 1.5

# 生成密集采样的x轴
x = np.linspace(345, 361, 500)

# 计算所有高斯峰的叠加总和
gaussian_sum = np.zeros_like(x)
for mu, amp in zip(wavelengths, intensities):
    gaussian_sum += gaussian(x, mu, sigma, amp)

# 绘图
plt.figure(figsize=(10, 6))
# 绘制delta竖峰
plt.vlines(wavelengths, ymin=0, ymax=intensities, color='blue', linewidth=2, label='Delta Peaks')
# 绘制高斯叠加曲线
plt.plot(x, gaussian_sum, color='red', linewidth=2, label=f'Gaussian Sum (sigma={sigma})')

plt.title('UV/VIS Spectrum: Delta Peaks + Gaussian Sum')
plt.xlabel('Wavelength (nm)')
plt.ylabel('Intensity')
plt.grid(True)
plt.legend()
plt.savefig('uv_spectrum_python.png', dpi=100)
plt.show()

运行脚本后会弹出绘图窗口并保存图片,修改sigma可调整高斯宽度,调整x的采样点数能优化曲线平滑度。


内容的提问来源于stack exchange,提问作者armando

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 20:27:31