gnuplot

GNUPLOT: stats gives wrong results on loop


I have a data file with matrices split into different gnuplot indices. I wanna do an animation of a density plot evolving with time (=index).

The problem is that I want to keep the maximum and minimum of the cbrange symmetric while allowing it to change with time.

In the code below, the first "stats" command simply gives me the number of blocks for the loop. The second "stats" command with prefix "B" should give me the max and min values for the matrix at each index, so I can set cbrange properly.

The first time the code enters the loop it works (for i=1) and stats gives me the proper numbers. Starting on the second loop (i=2) stats gives me wrong numbers...

I've tried to set cbrange and zrange to [*:*] before the stats command, but it doesn't help.

Here's the code:

set terminal gif animate delay 0.5
set output 'foobar.gif'
stats 'dat-rw2d.dat' nooutput

set pm3d map
set palette defined (-1 "blue", 0 "white", 1 "red")

print STATS_blocks

do for [i=1:int(STATS_blocks)] {
    print i

    stats "dat-rw2d.dat" index (i-1) matrix nooutput prefix "B"

    max = (B_max > -B_min)?(B_max):(-B_min)
    set cbrange [-max:max]

    print B_max, B_min

    splot 'dat-rw2d.dat' matrix index (i-1)
}

If I don't plot anything (code below), the stats give me the correct numbers. So it is actually the "splot" that is causing the problem. It's fixing some scale and getting in the way of stats? I've tried to set cbrange [*:*] before the stats, but it doesn't solve the problem.

do for [i=1:int(STATS_blocks)] {
    print i

    stats "dat-rw2d.dat" index (i-1) matrix nooutput prefix "B"

    max = (B_max > -B_min)?(B_max):(-B_min)
    set cbrange [-max:max]

    print B_max, B_min
}

Solution

  • If you don't specify any column to use for stats, gnuplot tries to guess a suitable default one. With the matrix option this seems to be a wrong one (probably x-value or y-value, or matrix size), which doesn't change from block to block.

    You must tell gnuplot to explicitely use the third column for the stats:

    stats 'dat-rw2d.dat' nooutput
    
    set pm3d map
    set palette defined (-1 "blue", 0 "white", 1 "red")
    
    print STATS_blocks
    
    do for [i=1:int(STATS_blocks)] {
        print i
    
        stats "dat-rw2d.dat" using 3 index (i-1) matrix nooutput prefix "B"
    
        max = (B_max > -B_min)?(B_max):(-B_min)
        set cbrange [-max:max]
    
        print B_max, B_min
    
        splot 'dat-rw2d.dat' matrix index (i-1)
    }