最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

gnuplot - To plot boxes with different colors depending on a given condition - Stack Overflow

programmeradmin1浏览0评论

I'm trying to plot the absolute values of a datablock (called $dif) with solid boxes, whose colors depend on whether the y-coordinate is greater or lower than zero, namely

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

I'm obviously making a syntax error since I'm getting the following error message: "colorspec option not recognized". Does anyone see what I'm doing wrong ?

I'm trying to plot the absolute values of a datablock (called $dif) with solid boxes, whose colors depend on whether the y-coordinate is greater or lower than zero, namely

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

I'm obviously making a syntax error since I'm getting the following error message: "colorspec option not recognized". Does anyone see what I'm doing wrong ?

Share Improve this question asked Mar 27 at 21:28 ifffamifffam 1879 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

If you want variable color depending on the data file you should use the option lc variable (please check help colorspec). The last given column in ... using 1:2:(<column or function for color>) will determine the color.

So, either use RGB codes (0xrrggbb) for the color

plot $Data u 1:2:($2>0?0x00ff00:0xff0000) w boxes lc rgb var

or since gnuplot 5.5


plot $Data u 1:2:($2>0?rgbcolor("green"):rgbcolor("red")) w boxes lc rgb var

As minimal complete example:

Script:

### variable colors
reset session

# create some random test data
set table $Data
    set samples 9
    plot '+' u 0:(rand(0)*10-5) w table
unset table

set key noautotitle
set boxwidth 0.9
set xrange noextend
set xzeroaxis lt -1
set style fill solid 0.5

plot $Data u 1:2:($2>0?0x00ff00:0xff0000) w boxes lc rgb var 
### end of script

Result:

发布评论

评论列表(0)

  1. 暂无评论