6

I've got a data.csv file which is structured like this:

n    John Smith stats     Sam Williams stats
1                23.4                   44.1
2                32.1                   33.5
3                42.0                   42.1

Currently I'm plotting with the following command in gnuplot:

plot 'data.csv' using 1:2 title 'John' with lines, '' using 1:3 title 'Sam' with lines

The question is how to retrieve first names from the first line of .csv rather than entering them manually?

In addition, is it possible to make it adjustable in case I add a column to the table, so it automatically adds another line with the appropriate title?

sashkello
  • 17,306
  • 24
  • 81
  • 109

2 Answers2

10

You say you have a csv file, so I assume your data file looks like this (and is saved in infile.csv):

n,John Smith stats,Sam Williams stats
1,23.4,44.1
2,32.1,33.5
3,42.0,42.1

If your version of Gnuplot is recent enough, you can use columnhead as the title argument:

echo "
  set datafile separator ','
  plot 'infile.csv' using 1:2 with lines title columnhead
" | gnuplot --persist

Or use the key option:

echo "
  set datafile separator ','
  set key autotitle columnhead
  plot 'infile.csv' using 1:2 with lines, '' using 1:3 with lines
" | gnuplot --persist

Edit - shorten headings

echo "
  set datafile separator ','
  set key autotitle columnhead
  plot '< sed -r \"1 s/,([^ ]+)[^,]+/,\1/g\" infile.csv' using 1:2 with lines, '' using 1:3 with lines
" | gnuplot --persist

Output:

Plot of column 1:2 and 1:3 of infile.csv

Note this answer to a follow-up question may also be relevant.

Community
  • 1
  • 1
Thor
  • 45,082
  • 11
  • 119
  • 130
  • columnhead would print the title of the whole column. It is too long and obstructs the plot (there are more and longer columns there), I need only first names shown - this is the problem. – sashkello Nov 14 '12 at 01:24
  • @sashkello: not sure if you can do that with Gnuplot commands, I've added one way you could do it with GNU sed. – Thor Nov 14 '12 at 01:33
1

You want to extract only a portion of the columnheader for the legend.

Update:

This is a task which you can easily do with gnuplot>=5.4.0. Check help columnheader and help word.

plot for [col=2:4] FILE u 1:col w l title word(columnheader(col),1)

However, the above command will not work with gnuplot versions 4.6.0 to 5.2.8. Whereas title columnheader(col) will work, but title word(columnheader(col),1) will not.

Workaround: (for gnuplot versions 4.6.0 to 5.2.8)

Again a strange gnuplot-only workaround. In short: In a plot for loop which starts at 1 you assign the header of column 2 to the variable myHeader, however, you are plotting nothing (NaN) with title myHeader='' (empty string will not generate a keyentry). In the next iteration you plot column 2, with the previously extracted header. This will continue until the last column (here: N=4).

Data: SO13371449.csv (some more example data added)

n,    John Smith stats,     Sam Williams stats,    Tom Muller stats
1,                23.4,                   44.1,                22.1
2,                32.1,                   33.5,                25.7
3,                42.0,                   42.1,                40.0

Script: (works for gnuplot>=4.6.0)

### get only a portion of columnheader for the title (gnuplot>=4.6.0)
reset

FILE = "SO13371449.csv"

set datafile separator ","
myHeader = ''
N=4
plot for [col=1:N] FILE u ($0==0 && col<N ? myHeader=word(strcol(col+1),1) : 0, \
     col==1 ? NaN : $1):col w lp pt 7 title myHeader
### end of script

Result: (created with gnuplot 4.6.0)

enter image description here

theozh
  • 22,244
  • 5
  • 28
  • 72