2

I have input like:

year   s1   s2  s3
2000   1    2   3 
2001   2    4   6 
2002   4    8   12

I don't know how many series. Today it's 3, tomorrow it may be 4.

I want to plot it in a multi-series chart. Something like this:

set key autotitle columnhead
plot 'data/chart-year-subreddit-count' using 1:2 with lines, \
'data/chart-year-subreddit-count' using 1:3 with lines, \
'data/chart-year-subreddit-count' using 1:4 with lines

Except since I don't know how many columns, I don't know what to put in my gnuplot script.

Do I have to write a script to write the file? Or can gnuplot work out how many series there are automatically?

Joe
  • 46,419
  • 33
  • 155
  • 245

2 Answers2

2

Gnuplot itself cannot count the number of columns, but you can use e.g. wc and head to count the number of columns:

file = 'data/chart-year-subreddit-count'
cols = int(system('head -1 '.file.' | wc -w'))
plot for [i=2:cols] file using 1:i with lines
Christoph
  • 47,569
  • 8
  • 87
  • 187
0

A bit late, but I disagree with Christoph, since gnuplot 5.0.0 (Jan 2015) was already able to count column via stats and the variable STATS_columns (check help stats).

stats FILE u 0 nooutput
plot for [col=2:STATS_columns] FILE u 1:col

Even for gnuplot 4.6.0 (Mar 2012) there is a gnuplot-only solution (and hence platform-independent).

Current versions:

At least for gnuplot>=5.0.4 (Jul 2016) you have the following option:

plot for [col=2:*] FILE u 1:col
theozh
  • 22,244
  • 5
  • 28
  • 72