4

I have to create a color-map, and the plotting style "with image" does exactly what I want. (plot the exact value of z at the position x,y, so using pm3d is not an option for me)

The problem is, there are undefined points in my datafile. For example, the function represents a mass ratio and therefore negative z-values have no physical meaning and I want to omit them. Or some z-values are even "NaN".

Example Datafile:

1.0   1.0    1.5
1.0   2.0    1.7
1.0   3.0    1.9
2.0   1.0    1.6
2.0   2.0    1.8
2.0   3.0    2.0
3.0   1.0    1.7
3.0   2.0    1.9
3.0   3.0   -1.0

So I don't want to plot the value -1 at the position (3,3) but leave the pixel at (3,3) blank.

I tried this:

plot './test.dat' u 1:2:($3>0 ? $3 : 1/0) with image

but it doesn't work. It says:

warning: Number of pixels cannot be factored into integers matching grid. N = 8 K = 3

set datafile missing "NaN"

in the case that the -1.0 is replaced by "NaN" doesn't work either.

the only alternative I found is:

set pointsize 10
plot './test.dat' u 1:2:($3>0 ? $3 : 1/0) palette pt 5

but then I have to adjust the pointsize, x- and y-range and plot size for every plot manually, so that there are no spaces or overlapping of the datapoints. (See this question.)

So to cut a long story short: is there any way to use the "with image" plotting style with undefined/missing data points and leaving this points white?

Community
  • 1
  • 1

2 Answers2

2

I haven't found a way to make gnuplot handle NaN nicely in this case. It sets it to 1 for me, which seems odd but may be a feature of how 'plot ... with image' handles missing data.

There is one trick you can use if you only want to eliminate negative numbers:

#!/usr/bin/env gnuplot

set terminal png 
set output 'test.png'

filter(x) = (x > 0) ? x : 1/0 
philter(x) = (x > 0) ? x : 0 

# just in case
set zero 1e-20

# make points set to zero be white
set palette defined (0 1.0 1.0 1.0, \
                 1e-19 0.0 0.0 1.0, \
                     1 1.0 0.0 0.0)

# get min/max for setting color range
stats 'test.dat' u (filter($3)) nooutput

# set color range so minimum value is not plotted as white
set cbrange [STATS_min*(1-1e-6):STATS_max]

plot './test.dat' u 1:2:(philter($3)) with image

On your datafile it produces this plot: enter image description here

It's not quite ideal since there is that white bit at the bottom of the color bar and it doesn't handle NaN. The reason it's impossible to get rid of the white bit is that when setting the palette, the numbers used are just autoscaled to fit whatever the colorbar is, and there are a discrete number of slots in the palette (256?). So, the first slot in the palette will always show the value for the start of the palette (white), regardless of whether the next color in the palette shows up 1e-19 of the way through the scale.

andyras
  • 15,542
  • 6
  • 55
  • 77
0

Just for the records, the solution would have been as simple as this: replace 1/0 by NaN.

With gnuplot 4.6.0 (March 2012), 1/0 will give the message warning: Number of pixels cannot be factored into integers matching grid. N = 8 K = 3 and will plot nothing, however, NaN seems to work fine.

With gnuplot>=5.0 both versions, 1/0 and NaN, will give the desired result.

Data: SO14235928.dat

1.0   1.0    1.5
1.0   2.0    1.7
1.0   3.0    1.9
2.0   1.0    1.6
2.0   2.0    1.8
2.0   3.0    2.0
3.0   1.0    1.7
3.0   2.0    1.9
3.0   3.0   -1.0

Script: (works for gnuplot>=4.6.0, March 2012)

### plot with image with blanked values
reset

FILE = "SO14235928.dat"

set size ratio -1
plot FILE u 1:2:($3>0 ? $3 : NaN) w image notitle
### end of script

Result: (created with gnuplot 4.6.0)

enter image description here

theozh
  • 22,244
  • 5
  • 28
  • 72