1

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

Andrea
  • 135
  • 6

1 Answers1

4

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

theozh
  • 22,244
  • 5
  • 28
  • 72
  • You may also want to have a look to the following answers with no separation between blocks: https://stackoverflow.com/a/64423162/7295599 and https://stackoverflow.com/a/64482390/7295599 – theozh Oct 26 '20 at 08:28
  • 1
    In the case of using $Data in your example, you could also use 'every' as follows. `plot for [i=0:2] $Data every :::i::i u 1:2 w lp pt 7 title sprintf("Set %d",i+1)` – binzo Oct 26 '20 at 11:39
  • @binzo, you're absolutely right... sorry for making things more complicated than they are. The blocks (or sets or whatever you call it) which you can address via `every` are seperated by exactly one empty line as in the OPs question. I will add it. – theozh Oct 26 '20 at 12:54