Search code examples
3dgnuplotheatmap

GnuPlot: Data Format for Heat Map?


I'm trying to do a heat map in GnuPlot. Here's the data:

enter image description here

I got it working with this data layout:

enter image description here

...but I had to round the Y Axis values to the nearest 5 to get them to fit in the exact rows of the grid.

So I'm trying to figure out a more 3D data layout. I tried something like this:

X    Y    Z
1   69  3.9

2   44  5.2

3   39  2.6

4   51  5.1

5   50  2.2

6   47  2.5

7   52  2.1

8   52  2.6

9   52  4.0

...but I got a "Singular matrix in LU-DECOMP" error.

I tried doing multiple rows in the data file for each value of Z, but I couldn't figure out what to put for the empty X and Y positions.

What's the correct way to lay out a data file like this?

For completeness, here's my command file:

unset surface
unset key
set view map
set pm3d
set palette defined (0 'black', 1 'gray40', 2 'dark-green', 3 '#fee000', 4 '#fc7a00', 5 '#ff0000')
set cbrange [0:5]
set output 'temp.jpg'

set term jpeg size 900, 900 enhanced font '/Library/Fonts/Arial.ttf' 12  #000000 Effffff
set dgrid3d 400,400 splines
set size square
set xtics("" 1, "X Label 1" 2, "X Label 2" 3, "X Label 3" 4, "X Label 4" 5, "X Label 5" 6, "X Label 6" 7, "X Label 7" 8, "X Label 8" 9, "X Label 9" 10)
unset label
unset title
set title '{/=18 Title}'  offset 0, .5
set ylabel '{/=18 Y Label}'  offset -1.6, 0
set xlabel '{/=18 X Label}'  offset 0,-3.0
set xtics out nomirror
set ytics out nomirror
set ytics("25" 0, "30" 1, "35" 2, "40" 3, "45" 4, "50" 5, "55" 6, "60" 7, "65" 8, "70" 9, "75" 10, "" 11)
set y2tics("25" 0, "30" 1, "35" 2, "40" 3, "45" 4, "50" 5, "55" 6, "60" 7, "65" 8, "70" 9, "75" 10, "" 11)

set xrange [0:11] 
set yrange [0:11]
unset colorbox 

set link x2; set link y2

splot [1:] '/gnuplot_data_test_v400.txt' matrix

replot


# http://hirophysics.com/gnuplot/gnuplot10.html
# https://stackoverflow.com/questions/17154364/gnuplot-pm3d-plot-triangle-data?rq=1
# https://askubuntu.com/questions/900501/i-cant-splot-with-pm3d-map-my-data-file-gnuplot
# http://lowrank.net/gnuplot/datafile-e.html#3dim
# http://psy.swansea.ac.uk/staff/carter/gnuplot/gnuplot_3d.htm


Update: Here's the kind of chart I'm trying to make:

enter image description here


Solution

  • It's not completely clear to me what your exact expectations are..., but I would use the plotting style with boxxyerror (check help boxxyerror).

    Code:

    ### heatmap with boxxyerror
    reset session
    
    # X    Y    Z
    $Data <<EOD
    1   69  3.9
    2   44  5.2
    3   39  2.6
    4   51  5.1
    5   50  2.2
    6   47  2.5
    7   52  2.1
    8   52  2.6
    9   52  4.0
    EOD
    
    myXLabels = '"Label A" "Label B" "Label C" "Label D" "Label E" "Label F" "Label G" "Label H" "Label I"'
    myXtic(col) = word(myXLabels,int(column(col)))
    BoxWidth = 1
    BoxHeight = 1
    xLow(col)  = column(col)-BoxWidth/2.
    xHigh(col) = column(col)+BoxWidth/2.
    yLow(col)  = column(col)-BoxHeight/2.
    yHigh(col) = column(col)+BoxHeight/2.
    
    set style fill solid 1.0
    plot $Data u 1:2:(xLow(1)):(xHigh(1)):(yLow(2)):(yHigh(2)):3:xtic(myXtic(1)) w boxxy fc palette notitle
    ### end of code
    

    Result:

    enter image description here

    Addition:

    Without knowing the details of your application, the following might be a starting point. Add some points in the corners with z=0. And you might want to "play" with the values in the line set dgrid3d 200,200 gauss kdensity 1.5.

    Code:

    ### heatmap with interpolation
    reset session
    
    # X    Y    Z
    $Data <<EOD
    1   69  3.9
    2   44  5.2
    3   39  2.6
    4   51  5.1
    5   50  2.2
    6   47  2.5
    7   52  2.1
    8   52  2.6
    9   52  4.0
    EOD
    
    # add some points in the corners with z=0
    set print $Data append
    do for [x=0:10:10] {
        do for [y=35:70:35] {
            print sprintf("%g %g %g",x,y,0)
        }
    }
    set print
    
    set view map
    set palette defined (0 'black', 1 'gray40', 2 'dark-green', 3 '#fee000', 4 '#fc7a00', 5 '#ff0000')
    set dgrid3d 200,200 gauss kdensity 1.5
    unset key
    set tics out
    
    splot $Data u 1:2:3 w pm3d
    ### end of code
    

    Result:

    enter image description here