Building columns by calling their header using GnuPlot

I have a file of this format:

x y1 y2 y3 ei1 ei2 ei3 es1 es2 es3 1 4 5 4 7 7 2 4 7 7 2 7 3 3 3 8 3 3 3 8 3 2 1 4 4 9 6 4 4 9 

I want to create graphs similar to what the following command give

 plot "filename" using 1:2:5:8 with yerrorbars 

but using column headers ( x , y1 , ei1 and es1 ) to call them. How can I do that?

Page 84 of the gnuplot manual (documenting the using command) states:

 Height Weight Age val1 val1 val1 ... ... ... 

then the following graph commands are equivalent

 plot 'datafile' using 3:1, '' using 3:2 plot 'datafile' using (column("Age")):(column(1)), \ '' using (column("Age")):(column(2)) plot 'datafile' using "Age":"Height", '' using "Age":"Weight" 

However, when I tried them, I only got row indices relative to myself.

+8
gnuplot
source share
1 answer

After carefully reviewing the documentation for gnuplot 4.4 vs gnuplot 4.6 (current stable version), it looks like the function you are trying to use was probably introduced in gnuplot 4.5 (Odd numbers are development branches - when they are considered stable, they increase to an even number). The only way that can come up with for this is to write a simple script in some other language that returns the column number (in stdout). Here is a simple python usage example, although I'm sure you can do it in awk if you want to stay in an all-POSIX environment:

 #python indexing is 0 based, gnuplot datafile indexing 1 based COL_AGE=`python -c 'print(open("datafile").readline().split().index("AGE")+1)'` COL_HEIGHT=`python -c 'print(open("datafile").readline().split().index("HEIGHT")+1)'` plot "datafile" u COL_AGE:COL_HEIGHT 

This little script does nothing interesting (it is assumed that the column headers are on the first line, for example), but using the power of python it would be pretty easy to extend the script further:

 #!/usr/bin/env python import sys with open(sys.argv[1]) as f for line in f: if (line.strip()): print (line.split().index(sys.argv[2])+1) sys.exit(0) 

Now you can call this script like: python script.py datafile AGE to find out which column is "AGE" in. This is an error if "AGE" is not in any column.

+5
source share

All Articles