16

My question is very simple. Suppose I have a datafile with column headers, like as follows

first second
1 1 
2 1
3 6
4 9

In gnuplot how do i make it so that the datafile is plotted using the column header as axis label? e.g. by calling

plot datafile using 1:2

i get the xaxis labeled first and the yaxis labeled second?

edit: I do know that I can use the column header as a key entry via set key auto title column head, however that's not quite what I'm looking for.

agamesh
  • 559
  • 1
  • 10
  • 26
Ferdinando Randisi
  • 4,068
  • 6
  • 32
  • 43

4 Answers4

8

To elaborate the suggestion of @andyras, here is how you can do it:

datafile = 'filename.txt'
firstrow = system('head -1 '.datafile)
set xlabel word(firstrow, 1)
set ylabel word(firstrow, 2)
plot datafile using 1:2

You must plot with the explicit using statement, otherwise gnuplot will complain about bad data on line 1.

Christoph
  • 47,569
  • 8
  • 87
  • 187
1

I don't think this feature is built in to gnuplot; you would probably have to use an awk-like utility to pull those labels out of a datafile.

You could try submitting a feature request on gnuplot's sourceforge site, and get feedback from the developers there.

andyras
  • 15,542
  • 6
  • 55
  • 77
0

Another question where people believe it can't be done with gnuplot. Here is a simple gnuplot-only solution without external tools, hence platform-independent.

Data: SO16089301.dat

first second
1 1 
2 1
3 6
4 9

Script: (works with gnuplot 4.6.0, March 2012)

### extracting axes labels from datafile
reset

FILE = 'SO16089301.dat'

stats FILE u (myX=strcol(1), myY=strcol(2)) every ::0::0 nooutput

set xlabel myX
set ylabel myY

plot FILE u 1:2 w lp pt 7 lc rgb "red"
### end of script

Result:

enter image description here

theozh
  • 22,244
  • 5
  • 28
  • 72
-2

I think it is supported. You are just supposed to be able to use "":

plot 'file' using "first":"second"

Although, if you want to do math in your using specification, you'll need the column("") function, too

plot 'file' using "first":(column("second")-(column("thrid"))

(Using only quoted header names with the math didn't work for me, anyway.)

  • 1
    With your method you can select the columns by the header strings, instead of using 1 and 2, but these headers aren't used as axis labels. – Christoph Aug 23 '13 at 13:58