Search code examples
gnuplot

2D plots from several input data files


My code is returning 1000 snapshot_XXXX.dat files (XXXX = 0001, 0002,...). They are two columns data files that take a picture of the system I am running at a specific time. I would like to mix them in the order they are created to build a 2D plot (or heatmap) that will show the evolution of the quantity I am following over time.

How can I do this using gnuplot?


Solution

  • Assuming you want the time axis going from bottom to top, you could try the following:

    n=4  # Number of snapshots
    
    set palette defined (0 "white", 1 "red")
    unset key
    set style fill solid
    
    set ylabel "Snapshot/Time"
    set yrange [0.5:n+0.5]
    set ytics 1
    
    # This functions gives the name of the snapshot file
    snapshot(i) = sprintf("snapshot_%04d.dat", i)
    
    # Plot all snapshot files.
    # - "with boxes" fakes the heat map
    # - "linecolor palette" takes the third column in the "using" 
    #   instruction which is the second column in the datafiles
    # Plot from top to bottom because each boxplot overlays the previous ones.
    
    plot for [i=1:n] snapshot(n+1-i) using 1:(n+1.5-i):2 with boxes linecolor palette
    

    This example data

    snapshot_0001.dat  snapshot_0002.dat  snapshot_0003.dat  snapshot_0004.dat
    1.0 0.0            1.0 0.0            1.0 0.0            1.0 0.0
    1.5 0.0            1.5 0.0            1.5 0.0            1.5 0.0
    2.0 0.5            2.0 0.7            2.0 0.7            2.0 0.7
    2.5 1.0            2.5 1.5            2.5 1.5            2.5 1.5
    3.0 0.5            3.0 0.7            3.0 1.1            3.0 1.5
    3.5 0.0            3.5 0.0            3.5 0.7            3.5 1.1
    4.0 0.0            4.0 0.0            4.0 0.0            4.0 0.7
    4.5 0.0            4.5 0.0            4.5 0.0            4.5 0.0
    5.0 0.0            5.0 0.0            5.0 0.0            5.0 0.0
    

    results in this image (tested with Gnuplot 5.0):

    evolution with time from bottom to top

    You can change the order of the plots if you want to go from top to bottom. If you want to go from left to right, maybe this can help (not tested).