Search code examples
plotgnuplotgif

Gnuplot : how to make a gif by plotting one block of data after the other in the same file .dat?


I would like to make a gif from a file data.dat:

   #x                     y                  z                   radius              color
#set 1
   222.333710          505.354645         -2938.58545          10.0000000          1.00000000         
   854.180481          64.3471069         -2844.13477          12.5992117          53.0000000    
  -109.606003          173.377197         -2975.83960          17.0997639          55.0000000       

#set 2
  0.746170461        -0.868437707         -2876.14355          123.856239          2001.00000    

#set 3   
   1.56590324E-02      6.23370660E-03     -2870.87378          129.126297          4001.00000          

At each time step I would like to plot:

splot "data.dat" using 1:2:3:4:5 with circles lc var notitle

for each set. Meaning at time 1 to plot the set 1, at time 2 the set 2 and so on.

The question of how to plot until a defined line has already been asked here. But how is it possible to plot until a line one doesn't know ? My idea is to code to plot until a blank line, but I'm open to any suggestion.

Eventually, in order to create the .gif I would write :

reset session

set term gif size 700,700 animate delay 30 optimize
set output "data.gif"

set xrange [-1000:1000]
set yrange [-1000:1000]
set zrange [-3000:-2500]

do for [a=1:3:1] {

###### code for splot
    
}
set output

Solution

  • If you separate your sets by two empty lines you can easily address them via index (check help index). If you don't know the number of sets (or blocks) you can do stats (check help stats). You will find the number of blocks in the variable STATS_blocks (to see all variables type show var STATS). Check the following example as starting point for further optimization.

    Attention: the option optimize in term gif might result in wrong colors (see: gnuplot: viewing angle dependent colors with 3D circles in GIF terminal) So, either don't optimize or export all frames as PNG (e.g. via term pngcairo) and use another software to create an animated GIF out of them.

    Code:

    ### create animation from unknown number of sub-blocks
    reset session
    set term gif size 400,400 animate delay 100
    set output "SO68940970.gif"
    
    # create some random test data
    set print $Data
        print "#x y z radius color
        SetCount = int(rand(0)*6)+10
        do for [b=1:SetCount] {
            print sprintf("#set%d",b)
            LineCount = int(rand(0)*5)+2
            do for [a=1:LineCount] {
                print sprintf("%g %g %g %g %g", \
                  rand(0)*2000-1000,rand(0)*2000-1000,rand(0)*500-3000, \
                  rand(0)*100+50, rand(0)*0xffffff)
            }
            if (b<SetCount) { print ""; print "" }  # two empty lines
        }
    set print
    
    stats $Data u 1 nooutput
    N = STATS_blocks
    print N
    
    set xrange [-1000:1000]
    set yrange [-1000:1000]
    set zrange [-3000:-2500]
    set ztics 250
    set view equal xyz
    
    set style fill solid 1.0
    do for [i=1:N] {
        set title sprintf("Set %d",i)
        splot $Data u 1:2:3:4:5 index i-1 w circles lc rgb var notitle 
    }
    set output
    ### end of code
    

    Result:

    enter image description here