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

gnuplot multiplot模式下仅最后一幅图可缩放的问题求助

解决gnuplot多子图(multiplot)的交互式缩放同步问题

问题描述

在gnuplot中创建5行1列的multiplot布局后,尝试对单个子图进行鼠标区域缩放时,所有子图消失,仅显示最后一幅缩放后的图;参考相关方案修改代码后,仍存在以下问题:

  • 缩放后需关闭窗口才能看到更新后的完整multiplot布局
  • 使用键盘命令(如Shift+L切换x轴对数/线性)时,需多次操作并点击鼠标才能同步更新所有子图

期望效果:对任意子图进行缩放/轴调整操作时,所有子图同步更新x轴范围,各子图y轴保持自动缩放。

初始代码

# visual commands
set terminal qt size 1300,1200

#---Axes
set format y "%.2f"

low_energy  = real(system('awk -F = ''{if (NR == 9) print $2 }'' 149Sm.sh'))
high_energy = real(system('awk -F = ''{if (NR == 10) print $2 }'' 149Sm.sh'))
set xrange [low_energy:high_energy]

#---Grid
set grid ytics mytics xtics mxtics  # draw lines for each ytics and mytics
set grid                            # enable the grid

#---Make the plot aesthetically better
set border lw 2
set key at graph 0.99, 0.99
set key font "Helvetica Bold,14"
set key box lw 1
set log x


set multiplot layout 5,1 columns
# Let's plot the sucker!

#Thin transmission
set title "Thin transmission"
set yrange [-0.1:1.2]
plot 'results/Sm149tThinFinal.asc' using ($1):($2):($3) with yerrorbars pt 7 ps 0.5 black title "Experimental", 'results/Sm149tThinFinal.asc' using ($1):($4) with lines lw 3 lc rgb 'red' title "SAMMY"

#Thick transmission
set title "Thick transmission"
set yrange [-0.1:1.2]
plot 'results/Sm149tThickFinal.asc' using ($1):($2):($3) with yerrorbars pt 7 ps 0.5 black title "Experimental", 'results/Sm149tThickFinal.asc' using ($1):($4) with lines lw 3 lc rgb 'red' title "SAMMY"

#J4 capture
set title "J4 capture"
set autoscale y
plot 'results/Sm149cJ4Final.asc' using ($1):($2):($3) with yerrorbars pt 7 ps 0.5 black title "Experimental", 'results/Sm149cJ4Final.asc' using ($1):($4) with lines lw 3 lc rgb 'red' title "SAMMY"

#J3 capture
set title "J3 capture"
set autoscale y
plot 'results/Sm149cJ3Final.asc' using ($1):($2):($2) with yerrorbars pt 7 ps 0.5 black title "Experimental", 'results/Sm149cJ3Final.asc' using ($1):($4) with lines lw 3 lc rgb 'red' title "SAMMY"

#Total capture
set title "Total capture"
set xlabel "Incident neutron energy (eV)" font "Helvetica Bold,14" offset 0,0,0
set autoscale y
plot 'results/Sm149cFinal.asc' using ($1):($2):($3) with yerrorbars pt 7 ps 0.5 black title "Experimental", 'results/Sm149cFinal.asc' using ($1):($4) with lines lw 3 lc rgb 'red' title "SAMMY"
   

unset multiplot

编辑后的尝试代码

# visual commands
set terminal qt size 1300,1200

#---Axes
set format y "%.2f"


low_energy  = real(system('awk -F = ''{if (NR == 9) print $2 }'' 149Sm.sh'))
high_energy = real(system('awk -F = ''{if (NR == 10) print $2 }'' 149Sm.sh'))
set xrange [low_energy:high_energy]

#---Grid
set grid ytics mytics xtics mxtics  # draw lines for each ytics and mytics
set grid                            # enable the grid

#---Make the plot aesthetically better
set border lw 2
set key at graph 0.99, 0.99
set key font "Helvetica Bold,14"
set key box lw 1
set log x

cond=1; while(cond) {
   set yrange [*:*]

set multiplot layout 5,1 columns
# Let's plot the sucker!

#Thin transmission
set title "Thin transmission"
set yrange [-0.1:1.2]
plot 'results/Sm149tThinFinal.asc' using ($1):($2):($3) with yerrorbars pt 7 ps 0.5 black title "Experimental", 'results/Sm149tThinFinal.asc' using ($1):($4) with lines lw 3 lc rgb 'red' title "SAMMY"

#Thick transmission
set title "Thick transmission"
set yrange [-0.1:1.2]
plot 'results/Sm149tThickFinal.asc' using ($1):($2):($3) with yerrorbars pt 7 ps 0.5 black title "Experimental", 'results/Sm149tThickFinal.asc' using ($1):($4) with lines lw 3 lc rgb 'red' title "SAMMY"

#J4 capture
set title "J4 capture"
set autoscale y
plot 'results/Sm149cJ4Final.asc' using ($1):($2):($3) with yerrorbars pt 7 ps 0.5 black title "Experimental", 'results/Sm149cJ4Final.asc' using ($1):($4) with lines lw 3 lc rgb 'red' title "SAMMY"

#J3 capture
set title "J3 capture"
set autoscale y
plot 'results/Sm149cJ3Final.asc' using ($1):($2):($2) with yerrorbars pt 7 ps 0.5 black title "Experimental", 'results/Sm149cJ3Final.asc' using ($1):($4) with lines lw 3 lc rgb 'red' title "SAMMY"

#Total capture
set title "Total capture"
set xlabel "Incident neutron energy (eV)" font "Helvetica Bold,14" offset 0,0,0
set autoscale y
plot 'results/Sm149cFinal.asc' using ($1):($2):($3) with yerrorbars pt 7 ps 0.5 black title "Experimental", 'results/Sm149cFinal.asc' using ($1):($5) with lines lw 3 lc rgb 'red' title "SAMMY"

   unset multiplot

   pause mouse button1,button2
   cond = MOUSE_KEY != 2   # exit on middle click

}

解决方案

修改后的完整代码

# 可视化设置
set terminal qt size 1300,1200 enhanced mouse

# 坐标轴格式
set format y "%.2f"

# 读取初始x范围
low_energy  = real(system('awk -F = ''{if (NR == 9) print $2 }'' 149Sm.sh'))
high_energy = real(system('awk -F = ''{if (NR == 10) print $2 }'' 149Sm.sh'))
# 定义全局x范围变量,用于同步所有子图
current_xmin = low_energy
current_xmax = high_energy

# 网格设置
set grid ytics mytics xtics mxtics
set grid

# 美化设置
set border lw 2
set key at graph 0.99, 0.99
set key font "Helvetica Bold,14"
set key box lw 1
set log x

cond=1; while(cond) {
    # 应用全局x范围,确保所有子图x轴同步
    set xrange [current_xmin:current_xmax]
    
    set multiplot layout 5,1 columns
        # 子图1:Thin transmission
        set title "Thin transmission"
        set yrange [-0.1:1.2]
        unset xlabel  # 隐藏非最后一幅图的x轴标签,避免重复显示
        plot 'results/Sm149tThinFinal.asc' using ($1):($2):($3) with yerrorbars pt 7 ps 0.5 black title "Experimental", \
             'results/Sm149tThinFinal.asc' using ($1):($4) with lines lw 3 lc rgb 'red' title "SAMMY"

        # 子图2:Thick transmission
        set title "Thick transmission"
        set yrange [-0.1:1.2]
        unset xlabel
        plot 'results/Sm149tThickFinal.asc' using ($1):($2):($3) with yerrorbars pt 7 ps 0.5 black title "Experimental", \
             'results/Sm149tThickFinal.asc' using ($1):($4) with lines lw 3 lc rgb 'red' title "SAMMY"

        # 子图3:J4 capture
        set title "J4 capture"
        set autoscale y
        unset xlabel
        plot 'results/Sm149cJ4Final.asc' using ($1):($2):($3) with yerrorbars pt 7 ps 0.5 black title "Experimental", \
             'results/Sm149cJ4Final.asc' using ($1):($4) with lines lw 3 lc rgb 'red' title "SAMMY"

        # 子图4:J3 capture
        set title "J3 capture"
        set autoscale y
        unset xlabel
        plot 'results/Sm149cJ3Final.asc' using ($1):($2):($2) with yerrorbars pt 7 ps 0.5 black title "Experimental", \
             'results/Sm149cJ3Final.asc' using ($1):($4) with lines lw 3 lc rgb 'red' title "SAMMY"

        # 子图5:Total capture
        set title "Total capture"
        set xlabel "Incident neutron energy (eV)" font "Helvetica Bold,14" offset 0,0,0
        set autoscale y
        plot 'results/Sm149cFinal.asc' using ($1):($2):($3) with yerrorbars pt 7 ps 0.5 black title "Experimental", \
             'results/Sm149cFinal.asc' using ($1):($5) with lines lw 3 lc rgb 'red' title "SAMMY"
    unset multiplot

    # 等待鼠标/键盘交互,支持缩放、平移、快捷键操作
    pause mouse button1,button2,keypress
    # 捕获交互后的x轴范围,更新全局变量
    current_xmin = GPVAL_X_MIN
    current_xmax = GPVAL_X_MAX
    # 鼠标中键点击退出循环
    cond = MOUSE_KEY != 2
}

关键修改说明

  1. 全局x范围同步:定义current_xmin和current_xmax存储当前x轴边界,每次循环绘制所有子图前统一应用该范围,确保所有子图x轴完全同步。
  2. 捕获交互后参数:利用gnuplot内置变量GPVAL_X_MIN和GPVAL_X_MAX获取鼠标缩放/平移后的x轴范围,更新全局变量后重新绘制所有子图。
  3. 优化交互触发:在pause mouse中添加keypress参数,确保键盘快捷键操作(如Shift+L切换轴类型)后立即触发重绘,无需重复操作。
  4. 布局细节优化:除最后一幅子图外,隐藏其他子图的x轴标签,避免重复显示,提升布局整洁度。

操作说明

  • 运行代码后,显示完整的5张子图布局
  • 对任意子图进行鼠标缩放/平移,松开鼠标后所有子图自动同步更新x轴范围,各子图y轴保持自动缩放
  • 使用键盘快捷键(如Shift+L切换x轴线性/对数、Shift+R重置轴范围)后,点击鼠标或任意按键即可同步更新所有子图
  • 点击鼠标中键可退出交互循环

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 05:30:53