Search code examples
gnuplot

Ploting a GIF from a single data file


Hello guys I am new to gnuplot and im looking to plot a gif representing the evolution of some probabilities with cycles. My data file is structured like this:

0   0   1   3.56133e-008    2   1.18619e-007    3   3.75373e-007 ...
0   0   1   3.56133e-008    2   2.26246e-008    3   1.44814e-007 ...

The first row represents cycle 0 while the first and second column represents position 0 and its probability. The number of positions is large so doing it manually will take too much time.

I got this for now but i dont really know how to do the for loop with this kind of data.

set terminal gif 
set output 'Probability.gif'
stats 'Probability.txt' nooutput
set xlabel 'Position'
set ylabel 'Probability'
set yrange [0:1]
set style fill solid border -1
unset key

Thanks beforehand any help is apreciated.


Solution

  • What you can do is to extract all x and y pairs from each row and plot it into a table. Actually, gnuplot prefers to have data in columns. Then plot this table to the gif terminal with the option animated. Check help gif. If you have a file remove the datablock $Data <<EOD ... EOD and in the code replace (2 times) $Data by "YourFilename".

    Code:

    ### animated rows x0 y0 x1 y1 x2 y2
    reset session
    
    $Data <<EOD
    0 0.00   1 0.10   2 0.20   3 0.40   4 0.20   5 0.10   6 0.00
    0 0.00   1 0.08   2 0.18   3 0.48   4 0.18   5 0.08   6 0.00
    0 0.00   1 0.05   2 0.16   3 0.58   4 0.16   5 0.05   6 0.00
    0 0.00   1 0.08   2 0.18   3 0.48   4 0.18   5 0.08   6 0.00
    0 0.00   1 0.10   2 0.20   3 0.40   4 0.20   5 0.10   6 0.00
    EOD
    
    stats $Data nooutput
    ColCount = STATS_columns
    RowCount = STATS_records
    
    set xrange[0:6]
    set yrange[0:1]
    
    set terminal gif size 400,400 animate delay 50 optimize
    set output "myAnimation.gif"
    
    do for [j=0:RowCount-1] {
        set table $Extract
            plot for [i=1:ColCount/2] $Data u (column(2*i-1)):(column(2*i)) every ::j::j w table
        unset table
        plot $Extract u 1:2 w lp pt 7 lw 2 title sprintf("Row %d",j)
    }
    set output
    ### end of code
    

    Result:

    enter image description here