colorsgnuplotpalette

Color line by the highest point in gnuplot


Gnuplot script:

set palette defined (0 "blue", 100 "green", 500 "red")
splot "data" using 1:2:3 with lines lc palette z

Data file:

1 1 1
1 2 1
1 3 500
1 4 1

This results in something like this:

_/\

What I got, is that the 2 slopes are green.

I want them to be red. Or at least reddish.

I suspect that the coloring strategy is to use the color of the midpoint of the line.

How can I change the coloring strategy? ("color by highest point")


ALTERNATIVE:

If it is not possible this way, it would also be acceptable if it is done with another linecolor option. I have not quite figured it out yet, but I know there is direct coloring.

However, if possible I would prefer a palette-based solution, where I can easily generate a color gradient, and not have to do it manually in the datafile.


Solution

  • Requires current gnuplot (version 5.4)

    Instead of coloring with lc palette, we will do our own lookup in the current palette to get an RGB value for that palette value and then tell the plot command to use that rgb value with lc rgb variable.

    The palette(cbvalue) function is new in 5.4, which is why you need a current gnuplot.

    Because we are no longer plotting referring to lc palette gnuplot does not autoscale or draw the colorbox by default, so we must explicitly give cbrange.

    $DATA << EOD
    1 1 1
    1 2 1
    1 3 500
    1 4 1
    EOD
    
    set palette defined (0 "blue", 100 "green", 500 "red")
    set cbrange [0:500]
    
    highest(z) = (ztmp=zold,zold=z, ztmp>z ? ztmp : z)
    
    zold = NaN
    splot $DATA using 1:2:3:( palette(highest($3)) ) with lines lc rgb variable
    

    enter image description here

    To avoid having to reinitialize zold before a replot, you might want to include it in the plot statement as an in-line definition:

    splot zold=NaN, $DATA using 1:2:3:( palette(highest($3)) ) with lines lc rgb variable