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
相关产品推荐
相关产品推荐

