如何修改gnuplot脚本实现多输入文件分目录输出EMF文件
修改Gnuplot脚本实现多文件分目录批量生成EMF文件
需求说明
我有三个输入文件:total.Cu.dat、total.Zn.dat、total.Mn.dat,每个文件需要生成31个同名EMF输出文件(例如0-2.emf),要求每个输入文件的输出存入对应专属目录(如Cu-emf目录对应total.Cu.dat的输出)。现有Gnuplot脚本仅能处理单个文件,需要修改为嵌套循环实现分目录存储。
原脚本
#!/usr/local/Cellar/gnuplot/5.4.10/bin/gnuplot ### Batch create EMF files from multiple subblocks set terminal emf font "{/:Bold}Sans,25" set terminal emf size 850,530 set border 15 front linecolor rgb "black" linewidth 3.000 dashtype solid set format x "%.1f" set format y "%.1f" set nokey set key top right outside reverse set key font "{/:Bold,15}" set xrange [0:1.23] set yrange [-2.2:2.2] set xlabel "U (V vs. RHE)" set xlabel offset character 0,0.6,0 font "{/:Bold},23" set ylabel "{/Symbol D}G (eV)" set ylabel offset character 1.0, 0, 0 font "{/:Bold},23" set xtics nomirror font "{/:Bold},24" set ytics nomirror font "{/:Bold},24" myFileOut(s) = sprintf("%s.emf",s) PATH = '/Users/cb/Desktop/DAC-phase/3d-TM/Cu-emf/' FILES = '/Users/cb/Desktop/DAC-phase/3d-TM/total.Cu.dat' do for [i=0:2] { stats FILES index i every ::::0 u (s=strcol(5),0) nooutput set output PATH.myFileOut(s) plot FILES index i u 1:2 w l lw 3 title columnheader(2), \ "" index i u 1:3 w l lw 3 title columnheader(3), \ "" index i u 1:2 w l lw 3 title columnheader(4) } set output
数据集示例
"potential" "Bare" "1/2ML OH Fe" "1/2ML OH Cu" "0-5" 0.00 0 0.649 0.000 1.23 0 -0.581 0.000 "potential" "Bare" "1/2ML OH Fe" "1/2ML OH Cu" "0-9" 0.00 0 0.297 0.000 1.23 0 -0.933 0.000 "potential" "Bare" "1/2ML OH Fe" "1/2ML OH Cu" "1-3" 0.00 0 -0.275 0.000 1.23 0 -1.505 0.000
修改后的脚本
#!/usr/local/Cellar/gnuplot/5.4.10/bin/gnuplot ### Batch create EMF files from multiple subblocks with directory separation # 配置绘图样式(保留原脚本设置) set terminal emf font "{/:Bold}Sans,25" size 850,530 set border 15 front linecolor rgb "black" linewidth 3.000 dashtype solid set format x "%.1f" set format y "%.1f" set nokey set key top right outside reverse font "{/:Bold,15}" set xrange [0:1.23] set yrange [-2.2:2.2] set xlabel "U (V vs. RHE)" offset character 0,0.6,0 font "{/:Bold},23" set ylabel "{/Symbol D}G (eV)" offset character 1.0, 0, 0 font "{/:Bold},23" set xtics nomirror font "{/:Bold},24" set ytics nomirror font "{/:Bold},24" myFileOut(s) = sprintf("%s.emf", s) # 定义元素列表、基础路径 elements = ["Cu", "Zn", "Mn"] basePath = '/Users/cb/Desktop/DAC-phase/3d-TM/' # 外层循环:遍历每个元素 do for [elem in elements] { # 动态生成当前元素的输入文件和输出目录路径 inputFile = basePath . 'total.' . elem . '.dat' outputDir = basePath . elem . '-emf/' # 内层循环:处理当前文件的31个数据块(索引0到30) do for [i=0:30] { # 从数据块首行第5列获取输出文件名 stats inputFile index i every ::::0 u (s=strcol(5),0) nooutput # 设置输出文件路径 set output outputDir . myFileOut(s) # 绘制图形(修复原脚本中第3个绘图项的列索引错误) plot inputFile index i u 1:2 w l lw 3 title columnheader(2), \ "" index i u 1:3 w l lw 3 title columnheader(3), \ "" index i u 1:4 w l lw 3 title columnheader(4) } } set output
关键修改说明
- 嵌套循环逻辑:外层循环遍历
Cu/Zn/Mn三个元素,内层循环处理每个文件中的31个数据块,实现批量处理 - 动态路径生成:通过元素名自动拼接输入文件路径和输出目录,避免重复写死路径,提升脚本复用性
- 修复绘图错误:原脚本第三个绘图项错误使用
u 1:2,改为u 1:4对应数据第4列,保证绘图准确性 - 分目录存储:每个元素的输出文件自动存入对应专属目录,无需手动调整路径重复执行脚本
内容的提问来源于stack exchange,提问作者Courtney Brea
相关产品推荐
相关产品推荐

