Search code examples
databasecsvplotgnuplottxt

Plotting from line to line with gnuplot


I have a .txt file in which two set of data are in the same column but divided by some characters, here an example:

#First set of data
#Time #Velocity
1 0.3
2 0.5
3 0.8
4 1.3

#Second set of data
#Time #Velocity
1 0.7
2 0.9
3 1.8
4 2.3

So I would like to plot this two set of data as two different curves, and also I do not know how many lines has each set of data ( or at least this number can change ) so i cannot use every command.( I'm looking for some gnuplot command, not bash command). Thank you


Solution

  • As you already mentioned every will not work here, since you have variable lengths of datasets (edit: yes it will, see edit below). In case you had two empty lines to separate your datasets you could use index, check help index. However, if you have a single empty line, pseudocolumn -1 will help. Check help pseudocolumns. Then you can define a filter with the ternary operator, check help ternary.

    Code:

    ### plotting variable datasets
    reset session
    
    $Data <<EOD
    #First set of data
    #Time        #Velocity
    1              0.3
    2              0.5
    3              0.8
    4              1.3
    
    #Second set of data
    #Time        #Velocity
    1              0.7
    2              0.9
    3              1.8
    
    #Third set of data
    #Time        #Velocity
    1              0.9
    2              1.4
    3              2.6
    4              3.6
    5              4.8
    EOD
    
    myFilter(col,i) = column(-1)==i-1 ? column(col) : NaN
    
    set key top left
    plot for [i=1:3] $Data u 1:(myFilter(2,i)) w lp pt 7 title sprintf("Set %d",i)
    ### end of code
    

    Edit: (as @binzo pointed out)

    Actually, I made it too complicated. As simple as the following will also do it without filter (filter can be used on other occasions). Note, the blocks are numbered starting from 0.

    plot for [i=1:3] $Data u 1:2 every :::i-1::i-1 w lp pt 7 title sprintf("Set  %d",i)
    

    Result:

    enter image description here