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

Gnuplot:拟合绘制含肩峰的复合峰,需单独绘制子峰

解决Gnuplot中拟合子峰单独绘制的问题

要单独画出拟合的子峰,核心是把每个子峰的函数单独定义,再在plot命令里添加上这些子峰的绘制项。以下是修改后的完整代码,关键改动已标注:

set term png
set fit quiet
set fit logfile "peakfitdatalog.txt"

# 定义基线和子峰函数,拆分后方便单独调用
baseline(x) = y0 + y1*x
voigt1(x) = a1 * ((2./pi)*w1/(4.*(x-x01)**2. + w1**2.))
voigt2(x) = a2 * ((2./pi)*w2/(4.*(x-x02)**2. + w2**2.))  # 修正原代码笔误:把w1改成w2
voigtfv(x) = baseline(x) + voigt1(x) + voigt2(x)  # 整体拟合函数由各部分组合

do for[i = 0:20] for [j = 0:20] {
    outfile = "testmap_Y".(sprintf('%02d_X%02d',j,i)).".png"
    set output outfile

    set xrange [900:1000]

    # 初始参数设置
    x01=960
    w1=10.64
    a1=50000
    mu1=0.90

    x02=967
    w2=10.64
    a2=500
    mu2=0.90

    y0=350
    y1=0.1

    fit voigtfv(x) "testmap_Y".(sprintf('%02d_X%02d',j,i)).".txt" via a1, a2, w1, w2, y0, y1

    # plot命令添加子峰和基线的绘制,用不同样式区分
    plot "testmap_Y".(sprintf('%02d_X%02d',j,i)).".txt" title "原始数据", \
         voigtfv(x) title "整体拟合峰" linetype 1 linewidth 2, \
         voigt1(x)+baseline(x) title "子峰1" linetype 2 linecolor rgb "#ff0000", \
         voigt2(x)+baseline(x) title "子峰2" linetype 3 linecolor rgb "#0000ff", \
         baseline(x) title "基线" linetype 4 linecolor rgb "#888888"
    
    set print "fit_param.dat" append
    print sprintf("%i,%i,%i,%i",a1,a2,w1,w2)
    set print
}

关键说明:

  • 拆分函数后,voigt1(x)和voigt2(x)是纯峰形部分,加上基线baseline(x)才是子峰在谱图中的实际位置
  • plot时通过title参数给每条曲线命名,用linetype和linecolor区分样式,避免混淆
  • 修正了原代码中第二个子峰分母的笔误(误用w1应为w2),保证拟合的准确性

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 16:46:21