You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Gnuplot柱状图:添加顺时针旋转90度的柱顶数值标签

Gnuplot柱状图添加旋转数值标签解决方案

需求说明

现有Gnuplot脚本生成柱状图,需实现:在每个柱子顶部添加对应高度的数值标签,并将标签顺时针旋转90度以适配布局。

原脚本

reset session

$Data <<EOD
"Push-Front"            2344 0x289e37
"Push-Back"             187  0x289e37
"Insert"                1222 0x289e37
"Push-Front-Collection" 519  0x289e37
"Push-Back-Collection"  420  0x289e37
"Insert-Collection"     287  0x289e37
"Search"                191  0x28579e
"Pop-Front"             4939 0xa83232
"Pop-Back"              251  0xa83232
"Delete"                4771 0xa83232
"Delete-Range"          345  0xa83232
EOD

set style line 11 lc rgb 0xeeeeee lt 1 lw 1
set style fill solid 1.0
set boxwidth 0.8
set key noautotitle
set ytics 1000
set grid y ls 11
set tics scale 0
set tics out
set xtics rotate by -45
set terminal svg size 450,300 enhanced font "Times New Roman,12"
set output '..\msc-sandbox\TotalDurationBarPlotArrayList.svg'
set yrange[0:5000]
set ylabel "Milliseconds"
plot $Data u 0:2:3:xtic(1) w boxes lc rgb var#, \
        '' u 1:2:3 w labels offset 0,0.7

修改后的脚本

reset session

$Data <<EOD
"Push-Front"            2344 0x289e37
"Push-Back"             187  0x289e37
"Insert"                1222 0x289e37
"Push-Front-Collection" 519  0x289e37
"Push-Back-Collection"  420  0x289e37
"Insert-Collection"     287  0x289e37
"Search"                191  0x28579e
"Pop-Front"             4939 0xa83232
"Pop-Back"              251  0xa83232
"Delete"                4771 0xa83232
"Delete-Range"          345  0xa83232
EOD

set style line 11 lc rgb 0xeeeeee lt 1 lw 1
set style fill solid 1.0
set boxwidth 0.8
set key noautotitle
set ytics 1000
set grid y ls 11
set tics scale 0
set tics out
set xtics rotate by -45
set terminal svg size 450,300 enhanced font "Times New Roman,12"
set output '..\msc-sandbox\TotalDurationBarPlotArrayList.svg'
set yrange[0:5200]  # 扩大y轴范围,避免最高标签超出图表
set ylabel "Milliseconds"
plot $Data u 0:2:3:xtic(1) w boxes lc rgb var, \
        '' u 0:2:(sprintf("%d", $2)) w labels rotate by 90 offset 0.5,1.2

关键改动说明

  • 移除原plot行末尾的注释符号#,启用标签绘制逻辑
  • 调整标签数据列:将u 1:2:3改为u 0:2:(sprintf("%d", $2)),确保标签与柱子x轴位置对齐,正确提取并格式化高度数值
  • 添加rotate by 90参数实现标签顺时针旋转90度
  • 修改offset 0.5,1.2让标签在柱子顶部居中,且避免遮挡柱子
  • 扩大yrange至[0:5200],防止最高柱子的标签超出图表可视范围

内容的提问来源于stack exchange,提问作者coderodde

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 05:40:31