Set color to grey for points below some cutoff when plotting with palette in Gnuplot

Viewed 424

Sorry for the long and probably poor title.

I am trying to plot some data in gnuplot, where the data file has the format

x       y       z
1.0     1.5     0.1
1.3     1.8     0.7
2.1     3.7     1.1
3.1     4.3     1.5
3.7     4.7     1.8

I want to plot y vs. x and, for each point, indicate the value of z by the color of the point. Currently I use the following code for this:

size = 0.5
type = 7
set term pngcairo 
set output 'Foo.png'
set palette rgbformulae 22,13,-31
set cblabel "z"
unset key
set xlabel 'x'
set ylabel 'y'
set title 'y vs. x'
plot "Data.txt" u 1:2:3 w p pt type ps size palette

This works, but I would like to make the following change: For each point whose z value is below a cutoff, say 1.0, I want the color to be set to grey, regardless of the z-value. Then for z-values greater than or equal to the cutoff, I want the color to be picked from the palette according to the z-value.

Thus, I want the color to go discontinuously from grey to blue (for the current palette) at the cutoff value. Can this be done, and if so how?

3 Answers

It is difficult to do with a palette based on rgbformulae. It's relatively easy with a defined palette. Here is an example that gives a palette close to what you chose, with a dark gray tacked on at the bottom end. cbrange is then set so that your z cutoff is the minimum. All z values below cbmin will be assigned the endpoint value, which is dark gray in this case.

set xrange [0:1]; set yrange [0:pi]
set sample 100; set isosample 100
set palette defined (-0.01 "grey20", 0 "blue", 0.4 "green", 0.6 "yellow", 1.0 "red")

zmin = -0.6
set cbrange [zmin: *]
set xyplane at zmin
splot cos(x)*sin(y+y*(x*x)) with pm3d

enter image description here

After gnuplot 5.4, this can be achieved by using the function "palette()" in "using" and combining it with the "with point lc rgb variable".

size = 3 ### 0.5 in original code 
type = 7
set term pngcairo 
set output 'Foo.png'
set palette rgbformulae 22,13,-31
set cblabel "z"
unset key
set xlabel 'x'
set ylabel 'y'
set title 'y vs. x'

set cbrange [0:2]  ### this is required by palette() function
gray = 0x808080    ### gray color expressed by integer

plot NaN w p palette, \
     "Data.txt" u 1:2:($3<1 ? gray : palette($3)) w p pt type ps size lc rgb variable

The RGB value passed to "lc rgb variable" are determined by comparing $3 with threshold value 1(for example).

"NaN w p palette" is a dummy plot, a technique for forcing the colorbox to appear. Without it, the colorbox will not be displayed.

enter image description here

Here is a gnuplot 5.2 version. You can use a rgbformulae palette. It uses the datablock $PALETTE which is created after carrying out the command test palette.

Code: (tested with gnuplot as low as 5.2.0)

### cut-off colors for a given rgbformulae palette (gnuplot >=5.2)
reset session

# create some test data
set xrange [-5:5]
set yrange [-5:5]
set samples 50
set isosamples 50
set table $Data 
    splot (sin(1.3*x)*cos(.9*y)+cos(.8*x)*sin(1.9*y)+cos(y*.2*x))*3
unset table

set palette rgbformulae 22,13,-31
test palette   # necessary to put the palette into the datablock $PALETTE
myColorMin = 0
myColorMax = 6
myOutOfRangeColor = 0x808080
myColorPos(c) = int(255.0 * (c-myColorMin)/(myColorMax - myColorMin))+1
myColor(c) = c<myColorMin || c>myColorMax ? myOutOfRangeColor : \
             (int(word($PALETTE[myColorPos(c)],2)*0xff)<<16) + \
             (int(word($PALETTE[myColorPos(c)],3)*0xff)<<8) + \
             (int(word($PALETTE[myColorPos(c)],4)*0xff))

set cbrange [myColorMin:myColorMax]
set view 36, 72
set ztics 5

splot $Data u 1:2:3:(myColor($3)) w pm3d  lc rgb variable notitle
### end of code

Result:

enter image description here

Related