Search code examples
animationmatrixgnuplotheatmap

gnuplot create heatmap animation with matrix and image


I try to create an animation of an heatmap I have to final result using : matrix and "with image" but I would like to create an animation point by point. I manage to do it column by column but not point by point

here my mwe


$Data <<EOD
2 1 9 3 9 4 4 9 1 4 1 
9 0 4 9 0 4 2 3 6 7 1 
1 8 5 5 2 1 5 4 1 5 9 
7 3 5 8 4 7 3 6 4 7 0 
9 0 6 5 5 9 0 5 0 0 2 
2 6 3 2 1 1 4 0 3 5 7 
9 2 6 8 9 2 2 5 1 5 2 
3 2 4 9 6 0 0 1 9 3 8 
6 7 1 9 1 0 8 9 1 7 6 
2 7 3 0 3 6 8 5 5 1 4 
9 0 2 6 4 6 9 4 5 0 8
EOD

stats $Data nooutput   # get the number of rows
lin = STATS_records
col = STATS_columns

nb_p = lin * col

print "lin: " . lin
print "col: " . col
print "nb_p: " . nb_p

do for [cur = 0 : nb_p] {
    plot $Data matrix using 1:2:3 every ::::cur with image title "n: ".cur
    pause 1
}

I also try with :

do for [c = 0 : col] {
    do for [l = 0 : lin] {
    plot $Data using c:l with points pt 5 title 'c: '.c.' l: '.l
    pause 1
    }
}

With a non-square matrix : there is an issue several lines are filled at same times and images is over but loop continue :

2 0 1 3 1 1 2 1 1 3 
1 0 0 2 3 0 3 0 1 0 
3 1 0 0 2 1 0 2 0 1 
2 3 0 3 0 0 3 2 0 2 
3 2 3 0 0 3 3 3 3 2 
1 1 1 2 0 2 3 2 2 1 

non-square sample


Solution

  • If I understood your question correctly, my first ideas would be:

    • analyze your matrix via stats $Data matrix noouput

    • STATS_records will contain the number of points

    • STATS_blank+1 will represent the number of rows

    • set the cbrange to a fixed range [STATS_min:STATS_max]

    • use a filter to plot only the desired datapoints depending on n

    In order to put each value into each square, you will see some "surprises". So, a simple with labels will not work. Check the modified script.

    Script:

    ### plot animated matrix point by point
    reset session
    
    $Data <<EOD
    2 1 9 3 9 4 4 9 1 4 1 
    9 0 4 9 0 4 2 3 6 7 1 
    1 8 5 5 2 1 5 4 1 5 9 
    7 3 5 8 4 7 3 6 4 7 0 
    9 0 6 5 5 9 0 5 0 0 2 
    2 6 3 2 1 1 4 0 3 5 7 
    9 2 6 8 9 2 2 5 1 5 2 
    3 2 4 9 6 0 0 1 9 3 8 
    6 7 1 9 1 0 8 9 1 7 6 
    2 7 3 0 3 6 8 5 5 1 4 
    9 0 2 6 4 6 9 4 5 0 8
    EOD
    
    stats $Data matrix nooutput   # get size and min/max of matrix
    N    = STATS_records
    rows = STATS_blank+1
    cols = N/rows
    cmin = STATS_min
    cmax = STATS_max
    
    set size ratio -1
    set xrange [-0.5:cols-0.5] noextend
    set yrange [-0.5:rows-0.5] reverse noextend
    set tics out
    set palette rgb 33,13,10
    set key at screen 0.20,0.95
    set cbrange[cmin:cmax]
    
    set term gif size 640,384 animate delay 5
    set output "SO79003207.gif"
    
    myFilter(n) = ($2*cols + $1) <= n ? $3 : NaN
    myLabel(n)  = (s=myFilter(n), s==s ? sprintf("%d",s) : '')
    
    do for [n = 0 : N-1] {
        plot $Data matrix u 1:2:(myFilter(n)) w image ti "n = ".n, \
                '' matrix u 1:2:(myLabel(n))  w labels notitle
    }
    set output
    ### end of script
    

    Result:

    enter image description here