Search code examples
gnuplot

How to plot a row as a line in GNUPLOT


I have seen similar questions asked a few years ago but the answers basically involved transposing the data before plotting. As GNUPLOT is evolving maybe there is a simpler way to handle this today?

My data are composition profiles at different time steps during a simulation. Each line represent the composition of an element at a specific time at different points in space and I would like to plot this row by row, in the simplest case just a single row as this

plot '-' using ?:? with lines ls 1
 0.310 0.318 0.334 0.364 0.413 0.476 0.532 0.569 0.588 0.595

e

As the simulation may take several hours (days) it would be nice to be able to see how the profiles evolve just by making the simulator generate such a row at certain time steps. For a snapshot at a specific time I could of course write the values in a column and "plot '-' using 0:1" but as I want to save several snapshots in the same file it is simpler to have each profile as a row, not just as an extremely long file with a single value at each row.
At the end I will be interested in plotting all the profiles at the saved time steps but then I could of course transpose the profile matrix.


Solution

  • If your data is a strict matrix (i.e. equal number of columns for each row), then you could plot a row like the following:

    Code:

    ### plotting rows
    reset session
    
    $Data <<EOD
     0.310 0.318 0.334 0.364 0.413 0.476 0.532 0.569 0.588 0.595
     1.310 1.318 1.334 1.364 1.413 1.476 1.532 1.569 1.588 1.595
     2.310 2.318 2.334 2.364 2.413 2.476 2.532 2.569 2.588 2.595
     3.310 3.318 3.334 3.364 3.413 3.476 3.532 3.569 3.588 3.595
    EOD
    
    myRow = 2     # counting starts from 0
    set key left
    
    plot $Data matrix u 1:3 every :::myRow::myRow w lp pt 7 lc "red" ti sprintf("Row number %d",myRow)
    ### end of code
    

    Result:

    enter image description here