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

基于gnuplot实现带数值标注的彩色柱状图及自定义图例需求

Gnuplot柱状图脚本修正方案

需求回顾

需要绘制4个柱状条的直方图,满足:

  • 每个柱子使用独特颜色
  • 柱子顶部标注对应数值
  • 右上角图例带浅灰背景、细黑边框
  • 补充需求:自定义图例背景/边框色、设置边框厚度、确保颜色对应正确、数值标注准确、最小改动现有代码

调整后的数据文件TotalDurationBarPlot.dat:

# ILL TL AL LL
934 3692 12274 48188

修正后的完整脚本

set title font "Monospaced,13" 'Total duration'
set grid
# 自定义图例样式:右上角、带框、浅灰背景、黑色细边框(线宽1)
set key right top box fillcolor rgb '#f0f0f0' linecolor rgb '#000000' linewidth 1
set style data histograms
set style histogram cluster gap 2
set style fill solid border 2
set xtics format ""
set grid ytics
set ylabel "Milliseconds"
set yrange [0:70000]

# 颜色定义(保留原变量名)
ArrayListColor   = "#491d75";
IndexedListColor = "#b32929";
TreeListColor    = "#12520b";
LinkedListColor  = "#d49435";

# 绘制柱子+顶部数值标注
plot 'TotalDurationBarPlot.dat' using 1 title "Indexed list" linecolor rgb IndexedListColor, \
     '' using 2 title "Tree list" linecolor rgb TreeListColor, \
     '' using 3 title "Array list" linecolor rgb ArrayListColor, \
     '' using 4 title "Linked list" linecolor rgb LinkedListColor, \
     # 标注每个柱子的数值,offset控制文字在柱子顶部上方
     '' u 1:($1):1 with labels offset 0,1.5 title "", \
     '' u 2:($2):2 with labels offset 0,1.5 title "", \
     '' u 3:($3):3 with labels offset 0,1.5 title "", \
     '' u 4:($4):4 with labels offset 0,1.5 title ""

set terminal png size 650,350 enhanced font "Monospaced,13"
set output 'TotalDuration.png'
replot
exit

关键改动说明

  1. 图例自定义样式:
    修改set key命令,添加box启用边框,fillcolor rgb '#f0f0f0'设置浅灰色背景,linecolor rgb '#000000'设置黑色边框,linewidth 1控制边框为细线条。

  2. 柱子颜色对应正确:
    保留原颜色变量与柱子标题的对应关系,确保每个列表类型匹配预设的独特颜色。

  3. 顶部数值准确标注:
    替换原单一标注项,新增4个标注命令,分别对应每一列的数值:

    • u X:($X):X 取第X列数值作为标注内容,放置在柱子顶部(y坐标为$X)
    • offset 0,1.5 将文字偏移到柱子上方,避免与柱子重叠
  4. 最小改动现有代码:
    保留原脚本的标题、网格、坐标轴设置,仅修改图例配置和标注部分,颜色变量和柱子绘制逻辑完全沿用。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 17:34:57