Gnuplot图例位置设置及多图布局对齐技术问题
扩展问题解决方案
要解决两张图绘图区域不统一、图例与y轴间距不一致的问题,核心是固定绘图区域的边距,让绘图区域在PNG中的位置和尺寸完全一致,再将图例锚定到绘图区域而非屏幕坐标系。
关键步骤:
- 固定绘图区域边距:用
set lmargin/rmargin/tmargin/bmargin指定屏幕坐标系下的固定边距,确保两张图的绘图区域完全对齐 - 锚定图例到绘图区域:继续用
graph坐标系设置图例位置,保证图例与绘图区域的相对位置固定
修改后的AddAtBeginning.plt代码:
set xlabel 'Size' set ylabel 'Microseconds' set xrange [0:1000000] # 固定绘图区域边距(屏幕坐标系:0=左/下,1=右/上) set lmargin at screen 0.15 # 左边距占屏幕15% set rmargin at screen 0.9 # 右边距占屏幕10%(从右往左算) set tmargin at screen 0.9 # 上边距占屏幕10% set bmargin at screen 0.15 # 下边距占屏幕15% plot 'AddAtBeginning.dat' index 0 title "Array list", \ '' index 1 title "Indexed list", \ '' index 2 title "Linked list", \ '' index 3 title "Tree list" # 修复缺失的闭合引号 set terminal png background rgb "grey" size 650,350 enhanced font "Monospaced,13" set output 'AddAtBeginning.png' set style rect fc rgb "white" fs set object 1 rectangle from screen 0,0 to screen 1,1 behind # 图例锚定到绘图区域右上角,偏移10像素(可按需调整) set key at graph 1,1 offset -10,-10 box lt -1 lw 2 opaque replot exit
修改后的AddAtEnd.plt代码:
set xlabel 'Size' set ylabel 'Microseconds' set xrange [0:1000000] # 与上一张图完全相同的边距设置 set lmargin at screen 0.15 set rmargin at screen 0.9 set tmargin at screen 0.9 set bmargin at screen 0.15 plot 'AddAtEnd.dat' index 0 title "Array list", \ '' index 1 title "Indexed list", \ '' index 2 title "Linked list", \ '' index 3 title "Tree list" # 修复缺失的闭合引号 set terminal png background rgb "grey" size 650,350 enhanced font "Monospaced,13" set output 'AddAtEnd.png' set style rect fc rgb "white" fs set object 1 rectangle from screen 0,0 to screen 1,1 behind # 完全相同的图例定位参数 set key at graph 1,1 offset -10,-10 box lt -1 lw 2 opaque replot exit
说明:
- 固定边距后,无论y轴刻度位数、标签长度如何变化,绘图区域在PNG中的位置和尺寸都保持一致
- 图例使用
graph坐标系定位,确保它与绘图区域的相对距离固定,自然实现两张图的图例与y轴间距统一
内容的提问来源于stack exchange,提问作者coderodde
相关产品推荐
相关产品推荐

