Search code examples
matrixplotgnuplotgif

Gnuplot: Making a gif of a map generated with matrix?


I generated a .dat file with 100 matrix 15x15, now I want to create a gif which shows the evolution from the first to the last matrix. They are all matrix with 1 or -1, so if I want to represent the inicial matrix I can copy and paste it in another file and I put this in gnuplot:

plot 'firstmatrix.dat' matrix with image

It represents the 1, -1 matrix with yellow and black.

To create the gif I'm trying to do this in gnuplot:

set terminal gif animate delay 20
set output 'evolution.gif'
set xrange [0:15]
set yrange [0:15]
N=15
nframes=5
do for [i=1:int(nframes)] {
  plot 'evolution.dat' every ::(i-1)*N+1::i*N matrix with image
}

I intend to read from the first line of the file to the 15th line, then from the 16th to the 30th and so on.

I put only 5 frames to see better the result, and I obtain that the gif shows the first matrix in the first frame and nothing more, only white frames.

The error message is four times this one:

warning: Skipping data file with no valid points

So the data for the first frame, the first matrix, is well processed but not the rest. So here is my problem, I don't know why it process good the first one and no more.

Thanks in advance.

It shows only the first matrix in the first frame


Solution

  • You've been pretty close. But it took me also some iterations and testing... Apparently, slicing a block of rows from a matrix requires every :::rowFirst::rowLast (mind the 3 colons at the beginning). And then gnuplot apparently takes the row index of the whole matrix as y-coordinate. Since you want to have it "on top of each other" you need the modulo operator % (check help operators binary). It might have been a bit easier if your matrices were separated by one or two empty lines.

    Code:

    ### animated matrix data
    reset session
    
    ### create some random data
    set print $Data
        do for [n=1:20] {
            do for [y=1:15] {
                Line = ''
                do for [x=1:15] {
                    Line=Line.sprintf("% 3g",int(rand(0)*2)*2-1)
                }
                print Line
            }
        }
    set print
    
    set terminal gif animate delay 30
    set output "tbMatrixAnimated.gif"
    unset key 
    N=15
    
    do for [i=1:20] {
        plot $Data u 1:(int($2)%N):3 matrix every :::N*(i-1)::N*i-1 with image 
    }
    set output
    ### end of code
    

    Result: (only 20 matrices)

    enter image description here