如何修改Gnuplot脚本使图例置于绘图外且不压缩绘图?
Gnuplot SVG图例外置不压缩绘图的修复方法
问题说明
使用Gnuplot生成SVG格式绘图时,设置图例(key)为外置(outside)后,主绘图区域会被压缩。由于SVG是矢量文件无法直接查看边距参数,常规调整方法无效,需要针对性修改脚本。
原始数据
"potential" "Bare" "1/2ML OH Fe" "1/2ML OH Cu" "1ML OH" "1ML OH Fe" "1ML OH Cu" "3/2ML OH Fe" "3/2ML OH Cu" "2ML OH" "0-2" 0.00 0 0.949 0 0 0 0 2.685 4.636 4.511 1.23 0 -0.281 0 0 0 0 -1.005 -0.454 -0.409 "potential" "Bare" "1/2ML OH Fe" "1/2ML OH Cu" "1ML OH" "1ML OH Fe" "1ML OH Cu" "3/2ML OH Fe" "3/2ML OH Cu" "2ML OH" "0-6" 0.00 0 0.267 0 0 0 0 2.632 3.960 4.710 1.23 0 -0.533 0 0 0 0 -1.058 0.370 -0.200 "potential" "Bare" "1/2ML OH Fe" "1/2ML OH Cu" "1ML OH" "1ML OH Fe" "1ML OH Cu" "3/2ML OH Fe" "3/2ML OH Cu" "2ML OH" "1-1" 0.00 0 -0.295 0 0 0 0 0.498 0.503 2.403 1.23 0 -1.605 0 0 0 0 -2.892 -2.087 -2.517
原始脚本
#!/usr/local/Cellar/gnuplot/5.4.4/bin/gnuplot ### Batch create SVG files from multiple subblocks FILE = "total.dat" set term svg font "{/:Bold}Sans,25" #set term qt font "Sans,21" set border 15 front linecolor rgb "black" linewidth 3.000 dashtype solid #set key noautotitle samplen 2 set format x "%.1f" #set nokey set key top right set key outside set key font "{/:Bold,15}" #set key at 2.3,2,0 set xrange [0:1.23] set yrange [-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},26" set xtics nomirror font "{/:Bold},24" set ytics nomirror font "{/:Bold},24" set title noenhanced myFileOut(s) = sprintf("%s.svg",s) do for [i=0:2] { stats FILE index i every ::::0 u (s=strcol(11),0) nooutput set output myFileOut(s) plot FILE 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), \ "" index i u 1:5 w l lw 3 title columnheader(5), \ "" index i u 1:6 w l lw 3 title columnheader(6), \ "" index i u 1:7 w l lw 3 title columnheader(7), \ "" index i u 1:8 w l lw 3 title columnheader(8), \ "" index i u 1:9 w l lw 3 title columnheader(9), \ "" index i u 1:10 w l lw 3 lc rgb "pink" title columnheader(10) } set output
修改后的脚本
#!/usr/local/Cellar/gnuplot/5.4.4/bin/gnuplot ### Batch create SVG files from multiple subblocks FILE = "total.dat" # 固定SVG画布尺寸并设置字体 set term svg font "{/:Bold}Sans,25" size 1000,700 #set term qt font "Sans,21" set border 15 front linecolor rgb "black" linewidth 3.000 dashtype solid #set key noautotitle samplen 2 set format x "%.1f" #set nokey # 手动设置边距:给右侧图例预留空间 set lmargin at screen 0.1 # 左侧边距占画布10% set rmargin at screen 0.75 # 右侧边距截止到画布75%,剩下25%给图例 set tmargin at screen 0.95 # 顶部边距 set bmargin at screen 0.15 # 底部边距 # 设置图例位置:右上角外置,合并参数避免冲突 set key top right outside font "{/:Bold,15}" #set key at 2.3,2,0 set xrange [0:1.23] set yrange [-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},26" set xtics nomirror font "{/:Bold},24" set ytics nomirror font "{/:Bold},24" set title noenhanced myFileOut(s) = sprintf("%s.svg",s) do for [i=0:2] { stats FILE index i every ::::0 u (s=strcol(11),0) nooutput set output myFileOut(s) plot FILE 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), \ "" index i u 1:5 w l lw 3 title columnheader(5), \ "" index i u 1:6 w l lw 3 title columnheader(6), \ "" index i u 1:7 w l lw 3 title columnheader(7), \ "" index i u 1:8 w l lw 3 title columnheader(8), \ "" index i u 1:9 w l lw 3 title columnheader(9), \ "" index i u 1:10 w l lw 3 lc rgb "pink" title columnheader(10) } set output
关键改动说明
- 固定SVG画布尺寸:通过
size 1000,700指定画布宽高,避免Gnuplot自动调整画布大小导致绘图区域压缩 - 手动设置屏幕边距:用
set *margin at screen命令直接基于画布比例分配空间,确保主绘图区域和图例各占固定比例,互不挤压 - 合并图例参数:将
set key top right、set key outside、set key font合并为一行,避免参数冲突,确保图例定位准确
内容的提问来源于stack exchange,提问作者Courtney Brea
相关产品推荐
相关产品推荐

