gnuplot

To plot boxes with different colors depending on a given condition


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 ?


Solution

  • 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:

    enter image description here