Multiple palettes and empty labels from file entries using matrix with image in gnuplot

1.2k views Asked by At

I have a file with a 4x4 score matrix and I'd like to plot the upper triangular with one color palette and the lower triangular with a different one, overlaying the score value (MWE at the bottom).

The original file looks like this

0.00 0.65 0.65 0.25
0.25 0.00 0.75 0.25
0.50 0.60 0.00 0.25
0.75 0.25 0.10 0.00

First, I created two separate files and used multiplot to have 2 different palettes.

FILE1 (upper triangular)

0.00 0.65 0.65 0.25
nan  0.00 0.75 0.25
nan nan 0.00 0.25
nan nan nan 0.00

FILE2 (lower triangular)

0.00 nan nan nan
0.25 0.00 nan nan
0.50 0.60 0.00 nan
0.75 0.25 0.10 0.00

Second, I plot the score values with

using 1:2:( sprintf('%.2f', $3 ) )

However, the 'nan' isn't interpreted as blank/empty and skipped but written onto the plot.

Any idea how to skip the nans and make gnuplot plot empty labels from individual entries of the data files?

The ternary operator in the following fashion do not seem to do the job

using 1:2:( $3 == 'nan' ? 1/0 : sprintf('%.2f', $3 ))

Thanks.


set multiplot
set autoscale fix
unset key

set datafile missing "nan"
set cbrange [0:1]
unset colorbox

set palette defined (0 "white", 0.1 "#9ecae1", 1.0 "#3182bd")

plot FILE1 matrix with image, \
    FILE1 matrix using 1:2:( sprintf('%.2f', $3) ) with labels font ',16'

set palette defined (0 "white", 0.1 "#a1d99b", 1.0 "#31a354")

plot FILE2 matrix with image, \
    FILE2 matrix using 1:2:( sprintf('%.2f', $3) ) with labels font ',16'

unset multiplot
1

There are 1 answers

1
Christoph On

You don't need to use multiplot and two separate files (I also couldn't get this working with the labels).

Just define a single palette, which contains as negative values one palette and as positive values the other palette. Based on the x and y-value from the single file you show first, you can now distinguish if the color value should be taken from the negative or from the positive palette part:

set autoscale fix
set cbrange [-1:1]
unset colorbox
unset key

set palette defined (-1.0 "#31a354", -0.1 "#a1d99b", 0 "white", 0.1 "#9ecae1", 1.0 "#3182bd")

plot 'FILE' matrix using 1:2:($1<$2 ? -$3 : $3) with image,\
     '' matrix using 1:2:(sprintf('%.2f', $3)) with labels font ',16'

enter image description here