3
votes

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
}
2
would adding set cbrange [*:*] after the splot help? - bibi
I've tried the set cbrange [*:*] and it doesn't work. I have read this tip somewhere else as well... but it doesn't work. - Gerson J Ferreira
In the help xrange (valid for all ranges) it says that you need an asterisk for autoscaling. - bibi
I know about the asterisk for autoscaling. The problem is that even enforcing autoscaling, the stats still gives the wrong answer after an splot command. It seems to be a real bug. - Gerson J Ferreira
to me it looks like a gnuplot bug, I even tried adding a reset and it still fails - bibi

2 Answers

3
votes

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)
}
0
votes

Since it looks like a bug, I can just propose a workaround (awful IMHO) but that's what comes to my mind:

Call gnuplot within a system command, save variables in a file dummy.txt and load that file from your script.

stats 'test.txt' nooutput
do for [cntr=1:int(STATS_blocks)] {

    # next line doesn't work 
    # stats 'test.txt' index (cntr-1) matrix prefix "B"

    # next 3 lines do the hack
    cmd=sprintf('gnuplot -e "stats \"test.txt\" index %d matrix nooutput prefix \"B\"; save var \"dummy.txt\""',cntr-1)
    system(cmd)
    load("dummy.txt")

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

    splot 'test.txt' matrix index (cntr-1) w l
}

If someone is willing to reproduce the problem , here is my test.txt file:

0 0 
0 1 


1 1 
1 2 


2 2 
3 3