如何修改gnuplot代码实现循环生成30个自定义标题的SVG绘图?
问题需求
需要生成30个SVG格式的绘图文件,每个文件对应step-1到step-30的输入数据文件,要求用for循环实现。同时需要修改两处标题:
- 全局标题
set title "Associative Pathway (0,1)",每个绘图的标题要不同 - 绘图中的红色曲线标题
title "{/:Bold 0.84 V}",每个绘图的该标题也要不同
原gnuplot代码如下:
#!/usr/local/Cellar/gnuplot/5.4.4/bin/gnuplot set termoption font "Sans,22" set border 15 front lt black linewidth 3.000 dashtype solid set xrange [0:5.5] set yrange[-5:0.1] set title "Associative Pathway (0,1)" set title font "{/:Bold},23" set xlabel "Reaction Coordinate" set xlabel font "{/:Bold,23}" set ylabel "Free Energy (eV)" set ylabel font "{/:Bold,23}" set xtic scale 0 set ytics out nomirror set xtics nomirror set xtics ("O_{2}" 0.3, "O@^*_{2}" 1.3, "OOH^*" 2.3 , "O^*" 3.3, "OH^*" 4.3 ,"H_{2}O" 5.3 ) font "{/:Bold},23" set ytics font "{/:Bold},23" ## Last datafile plotted: "step-x" p "step-1" u 7:8 w l dt 4 lc rgb 'blue' notitle, "step-1" u 2:3 w l lw 3 lc rgb 'blue' title "{/:Bold 0 V}" rep "step-1" u 9:10 w l dt 4 lc rgb 'red' notitle, "step-1" u 4:5 w l lw 3 lc rgb 'red' title "{/:Bold 0.84 V}" # EOF
数据文件示例:
#X-title #X-U(0) #X-U(1.8) #y-title #y-U(0) #y-U(1.8) #1 #2 #3 #4 #5 #6 #7 #8 #9 #10 O_{2} 0 0 0 -3.00 O_{2} 0.5 0 0.5 -3.00 O@^*_{2} 0.5 0 0.5 -3.00 O_{2} 1 -0.1 1 -3.04 O@^*_{2} 1 -0.1 1 -3.04 OOH^* 1.5 -0.1 1.5 -3.04 OOH^* 1.5 -0.1 1.5 -3.04 OOH^* 2 -1.00 2 -3.12 OOH^* 2 -1.00 2 -3.12 O^* 2.5 -1.0 2.5 -3.12 O^* 2.5 -1.00 2.5 -3.12 O^* 3 -2.00 3 -4.00 O^* 3 -2.00 3 -4.00 OH^* 3.5 -2.00 3.5 -4.00 OH^* 3.5 -2.00 3.5 -4.00 OH^* 4 -4.00 4 -4.50 OH^* 4 -4.00 4 -4.50 H_{2}O 4.5 -4.00 4.5 -4.50 H_{2}O 4.5 -4.00 4.5 -4.50 H_{2}O 5 -4.50 5 -4.50 H_{2}O 5 -4.50 5 -4.50 H_{2}O 5.5 -4.50 5.5 -4.50
解决方案
下面是修改后的gnuplot脚本,实现了循环生成30个SVG文件,并动态设置标题:
#!/usr/local/Cellar/gnuplot/5.4.4/bin/gnuplot # 全局样式设置,只执行一次 set termoption font "Sans,22" set border 15 front lt black linewidth 3.000 dashtype solid set xrange [0:5.5] set yrange[-5:0.1] set xlabel "Reaction Coordinate" font "{/:Bold,23}" set ylabel "Free Energy (eV)" font "{/:Bold,23}" set xtic scale 0 set ytics out nomirror set xtics nomirror set xtics ("O_{2}" 0.3, "O@^*_{2}" 1.3, "OOH^*" 2.3, "O^*" 3.3, "OH^*" 4.3, "H_{2}O" 5.3) font "{/:Bold},23" set ytics font "{/:Bold,23}" # 定义每个step对应的电压值数组,可根据实际需求修改数值 voltages = [0.84, 0.86, 0.88, 0.90, 0.92, 0.94, 0.96, 0.98, 1.00, 1.02, \ 1.04, 1.06, 1.08, 1.10, 1.12, 1.14, 1.16, 1.18, 1.20, 1.22, \ 1.24, 1.26, 1.28, 1.30, 1.32, 1.34, 1.36, 1.38, 1.40, 1.42] # 循环遍历1到30,处理每个step文件 do for [x=1:30] { # 设置输出为对应名称的SVG文件 set terminal svg enhanced font "Sans,22" set output sprintf("step-%d.svg", x) # 动态设置全局标题,示例格式为"Associative Pathway (x, x+1)",可按需修改 set title sprintf("Associative Pathway (%d, %d)", x, x+1) font "{/:Bold},23" # 获取当前step对应的电压值 current_voltage = voltages[x] # 动态调用数据文件并设置曲线标题 plot sprintf("step-%d", x) u 7:8 w l dt 4 lc rgb 'blue' notitle, \ sprintf("step-%d", x) u 2:3 w l lw 3 lc rgb 'blue' title "{/:Bold 0 V}", \ sprintf("step-%d", x) u 9:10 w l dt 4 lc rgb 'red' notitle, \ sprintf("step-%d", x) u 4:5 w l lw 3 lc rgb 'red' title sprintf("{/:Bold %.2f V}", current_voltage) } set output # 关闭输出文件
关键说明
- 循环实现:使用
do for [x=1:30]遍历1到30,每次循环处理一个step-x数据文件 - 动态输出文件名:通过
sprintf("step-%d.svg", x)自动生成对应名称的SVG输出文件 - 动态全局标题:利用
sprintf格式化字符串,将循环变量x代入标题,可调整格式适配需求 - 动态曲线标题:提前定义
voltages数组存储每个step对应的电压值,循环中取出对应值并格式化标题 - 样式复用:将全局样式设置放在循环外,避免重复执行,提升脚本运行效率
内容的提问来源于stack exchange,提问作者Courtney Brea
相关产品推荐
相关产品推荐

