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

Gnuplot直方图:基于数据列条件为特定柱状条着色

基于数据列字符串属性给Gnuplot直方图特定柱子着色

解决方案代码

reset ;
set title "testing color of certain histogram bars \n \
based on property in data file (column 4)"
set bars back ;
set style data histogram ;
set style histogram cluster;
set style fill solid border -1 ;
set grid back ;
set grid xtics ytics mytics mxtics ;
set style line 2 lt 1 lc rgb "black" ;

# 定义颜色变量,方便维护
DEFAULT_COLOR = "#0000ff"
TARGET_COLOR = "#00ff00"

plot newhistogram "", \
'test_08oct22a.dat' u 2:xticlabels(1):(strcol(4) eq "that" ? TARGET_COLOR : DEFAULT_COLOR) t "Column 2" lc rgb var, \
'' u 3:xticlabels(1):(strcol(4) eq "that" ? TARGET_COLOR : DEFAULT_COLOR) t "Column 3" lc rgb var

关键逻辑说明

  • 字符串匹配处理:用strcol(4)读取第4列的字符串,Gnuplot中字符串比较必须用eq运算符,不能用==,通过strcol(4) eq "that"判断是否命中目标条件。
  • 动态着色逻辑:在using语句中新增第三列作为颜色参数,通过三元表达式(条件 ? 匹配时颜色 : 默认颜色)动态选择颜色,再配合lc rgb var让Gnuplot识别并应用该颜色值。
  • 颜色参数化:提前定义颜色变量让代码更易修改,也可以直接在三元表达式中写入颜色字符串(比如"#00ff00")。

常见问题排查

  1. 之前尝试数值条件着色无效,是因为第4列是字符串类型,必须用strcol()处理字符串,不能用数值比较逻辑。
  2. 必须将lc rgb var放在每个plot项的末尾,确保Gnuplot正确识别颜色参数列。
  3. 你的Gnuplot 5.4 patchlevel 2版本完全支持strcol()和字符串eq比较,无需版本升级。

内容的提问来源于stack exchange,提问作者Bryan-StackExchange

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 00:35:22