Search code examples
matrixcolorsgnuplotcolorboxpalette

gnuplot "matrix with image" with a fixed colour for a certain value


With gnuplot, I am plotting a matrix stored in a file with the following commands:

set title "Matrix"
set xrange[-0.5:9.5]
set yrange[9.5:-0.5]
set pm3d map
unset key
unset surface
set term postscript eps enhanced color
set out "matrix.eps"
set palette defined (-1 "#A52A2A", 0 "white", 1 "green" )
splot "matcorrel" matrix with image

The matrix has positive and negative values, and I would like to put the zero values always in white, positive values in the green zone of the palette and negative values in brown. The positive values are bigger than the negative ones, so gnuplot is not placing the zero in white.

I have tried with set cbrange but I have only managed to modify the extreme colors, not being able to fix the central one.

Any ideas? Thank you very much in advance


Solution

  • Gnuplot cannot autoscale symmetrically around some value. You must use e.g. stats to determine the cbrange yourself:

    set autoscale xfix
    set autoscale yfix
    unset key
    
    set term postscript eps enhanced color
    set out "matrix.eps"
    
    stats "matcorrel" matrix using 3 nooutput
    cbmax = (abs(STATS_min) > abs(STATS_max) ? abs(STATS_min) : abs(STATS_max))
    set cbrange [-cbmax:cbmax]
    set palette defined (-1 "#A52A2A", 0 "white", 1 "green" )
    
    plot "matcorrel" matrix with image
    

    If you want to use different limits for the positive and negative values, but keep the zero in white you can use

    stats "matcorrel" matrix using 3 nooutput
    set cbrange [STATS_min:STATS_max]
    set palette defined (STATS_min "#A52A2A", 0 "white", STATS_max "green" )
    

    Note, that you don't need to use pm3d if you plot with image. Since you're plotting a heatmap, you can directly use plot.