2

I want to plot individual blocks from a file separately. I've found that this works:

$ cat test
A
0 0
1 1


B
0 0
1 2


C
0 0
1 3

Then

gnuplot> plot for [i=0:2] 'test' index i with lines title columnhead

gives me three separate lines labeled A,B,C. Perfect.

But I don't know the number of blocks in the file, so I have problems. This answer suggests using stats to get the number of blocks in a file, but stats fails with bad data on line 6 (the title, "B", of the second block).

I have control over the input file, so I can format that differently if needed.

How can I plot an arbitrary number of blocks (individually) with automatic titling?

Community
  • 1
  • 1
jake
  • 361
  • 1
  • 14

1 Answers1

2

With gnuplot version 5.0 you can use set key autotitle columnheader before you call stats:

set key autotitle columnheader
stats 'test'

set style data lines
plot for [i=0:STATS_blocks-1] 'test' index i 

That doesn't work with 4.6 and earlier versions. Here, you can use an external tool like grep to filter the data when passed to stats. To remove all lines which don't start with a digit or aren't empty one can use

stats '< grep -v "^[^0-9]" test'

or

stats '< grep ''\(^[0-9]\)\|\(^$\)'' test'

to determine the number of data sets, which is stored in the variable STATS_blocks.

Christoph
  • 47,569
  • 8
  • 87
  • 187
  • `set key autotitle columnheader` doesn't issue any errors when I try this in gnuplot 4.6. Are you saying that the behavior has changed in 5.0? – jake Feb 19 '15 at 21:07
  • When I use `set key autotitle columnheader; stats 'test'` I get the error `Bad data on line 6`, which is where the second title is. With 5.0 I don't get such an error, and `stats` returns me the number of data blocks. Alternatively you can use `grep` to filter out all lines which don't start with a digit or aren't empty, like `stats '< grep -v ''^[^0-9]'' test.dat'` (or `stats '< grep ''\(^[0-9]\)\|\(^$\)'' test.dat'`). That works also with earlier versions. – Christoph Feb 19 '15 at 21:19
  • `grep` did the trick, do you want to post that as a separate pre 5.0 answer? – jake Feb 19 '15 at 21:37