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

gnuplot按条件设置箱体颜色报错:colorspec option not recognized

问题

我尝试绘制名为$dif的数据块的绝对值,使用实心箱体,箱体颜色根据原y坐标($2)是否大于等于0分别设为绿色或红色,所用命令为:

plot $dif u (timecolumn(1,"%d/%m/%Y")):(abs($2)) w boxes fs solid 0.7 fc (($2)>=0 ? "green":"red") axis x1y2 notitle

但出现错误提示“colorspec option not recognized”,请问存在何种语法错误?

原因与解决方法

  • gnuplot不支持直接在fc(fillcolor)参数后使用字符串类型的三元表达式指定颜色,这是触发错误的核心原因。
  • 有两种可行的解决方式:
    1. 拆分绘图命令:分别筛选正负值数据,绘制对应颜色的箱体,逻辑直观易维护:
      plot $dif u (timecolumn(1,"%d/%m/%Y")):(abs($2)) w boxes fs solid 0.7 fc "green" axis x1y2 notitle if ($2)>=0, \
           $dif u (timecolumn(1,"%d/%m/%Y")):(abs($2)) w boxes fs solid 0.7 fc "red" axis x1y2 notitle if ($2)<0
      
    2. RGB数值映射:将颜色转换为十六进制RGB值,配合fc rgb variable在单条命令中实现,通过第三列返回对应RGB值:
      plot $dif u (timecolumn(1,"%d/%m/%Y")):(abs($2)):(($2)>=0 ? 0x00FF00 : 0xFF0000) w boxes fs solid 0.7 fc rgb variable axis x1y2 notitle
      
      其中0x00FF00对应绿色,0xFF0000对应红色,gnuplot可识别这类十六进制颜色值。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 16:20:03