如何为Gnuplot折线图的每个数据点添加数值标签?
在Gnuplot折线图的数据点上方标注数值
嘿,这事儿不难,只需要在你的Gnuplot脚本里给plot命令添加一个专门用来绘制标签的绘图项就行。我帮你修改了原脚本,直接就能用:
set title "df -m command test" set terminal png truecolor size 720,480 background rgb "#eff1f0" set output "/xxx/xxx/xxx/test.png" set grid set style line 1 \ linecolor rgb '#0060ad' \ linetype 1 linewidth 2 \ pointtype 7 pointsize 1.5 set offsets 0.5,0.5,0,0.5 set datafile separator "," set ylabel " MB BLOCK " set xlabel " Date " set format y "%g" # 核心修改:添加with labels绘图项来标注数值 plot "/xxxx/xxx/xxx/TEST.txt" using 2:xtic(1) with linespoints linestyle 1 title "MB used", \ "/xxxx/xxx/xxx/TEST.txt" using 1:2:2 with labels offset 0,1 font ",8" notitle
关键修改说明:
- 第二个绘图项
using 1:2:2:指定用第1列作为x坐标、第2列作为y坐标,同时把第2列的数值作为标签内容 offset 0,1:让标签在数据点正上方偏移一点(x方向不动,y方向向上移1个单位),避免和数据点重叠font ",8":可以调整标签的字体大小,这里设为8号字,你可以根据图表尺寸自行修改notitle:因为这个绘图项只是用来加标签,不需要在图例里显示新条目
进阶优化:格式化数值显示
如果想让标签的数值格式更统一(比如固定显示两位小数),可以用sprintf函数来格式化输出:
plot "/xxxx/xxx/xxx/TEST.txt" using 2:xtic(1) with linespoints linestyle 1 title "MB used", \ "/xxxx/xxx/TEST.txt" using 1:2:(sprintf("%.2f", $2)) with labels offset 0,1 font ",8" notitle
这样生成的图就会在每个折线点的上方清晰显示对应的MB数值啦~
内容的提问来源于stack exchange,提问作者Makmy
相关产品推荐
相关产品推荐

