从数据文件中放置标签的一个好方法是使用labels 绘图样式。但是,将标签放置在实际绘图区域之外存在一些困难,因为这些点和标签通常会被剪裁。
由于您无论如何都在使用stats 来修复 x 和 y 范围,因此我将这样做:
设置一个固定的右边距,例如set rmargin 20。这使用 20 个字符宽度的右边距。您也可以使用像set rmargin at screen 0.8 这样的绝对坐标,但由于您需要边距来放置标签,因此字符单位似乎是合适的。
使用绘图区域的右上角作为参考点(STATS_max_x, STATS_max_y),并使用offset 参数移动标签并再次移动一些字符宽度。
因此,一个完整的脚本可能如下所示:
# calculate the number of points
stats 'file.txt' using 1:2 nooutput
# if you want to have a fixed range for all plots
set xrange [STATS_min_x:STATS_max_x]
set yrange [STATS_min_y:STATS_max_y]
set terminal pngcairo size 800,400
outtmpl = 'output%07d.png'
v_label(x, y) = sprintf('vx = %.2f vy = %.2f', x, y)
c_label(x, y) = sprintf('x = %d y = %d', x, y)
t_label(t) = sprintf('t = %.2f', t)
set rmargin 20
do for [i=0:STATS_records-1] {
set output sprintf(outtmpl, i)
plot 'file.txt' every ::::i with lines title sprintf('n = %d', i),\
'' every ::i::i using (STATS_max_x):(STATS_max_y):(t_label($3)) with labels offset char 11,-5 notitle,\
'' every ::i::i using (STATS_max_x):(STATS_max_y):(c_label($1, $2)) with labels offset char 11,-6.5 notitle,\
'' every ::i::i using (STATS_max_x):(STATS_max_y):(v_label($4, $5)) with labels offset char 11,-8 notitle
}
set output
请注意,rmargin 和 offset 设置取决于终端、终端大小、字体和字体大小。为了更好地放置标签,您可以考虑分别放置 vx 和 vy 标签,并可能更改它们的对齐方式。
或者,在每次迭代中,您可以从数据文件中提取当前行并手动设置标签。但是,这需要您使用外部工具来提取该行:
do for [i=0:STATS_records-1] {
line = system(sprintf("sed -n %dp file.txt", i+2))
set label 1 at screen 0.9, screen 0.9 sprintf("t = %.2f", real(word(line, 3)))
set label 2 at screen 0.9, screen 0.88 sprintf("x = %.2f y = %.2f", real(word(line, 1)), real(word(line, 2)))
plot 'file.txt' every ::::i with lines title sprintf('n = %d', i)
}
我不知道哪种变体更适合您。我使用i+2 作为行号来跳过注释的标题行,它不会自动检测到。通过使用标签 (set label 1) 的标签,您可以确保旧标签被覆盖。