Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting columns by calling their header with GnuPlot

Tags:

gnuplot

I have 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 produce plots similar to what the following command would give

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

but using the columns headers(x, y1, ei1 and es1) to call them. How can this be done?

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

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

then the following plot commands are all 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 the row indices versus themselves.

like image 336
M. Toya Avatar asked Jul 16 '12 08:07

M. Toya


People also ask

How use gnuplot to plot data from a file?

To plot functions simply type: plot [function] at the gnuplot> prompt. Discrete data contained in a file can be displayed by specifying the name of the data file (enclosed in quotes) on the plot or splot command line. Data files should have the data arranged in columns of numbers.

What is gnuplot Splot?

splot is the command for drawing 3-d plots (well, actually projections on a 2-d surface, but you knew that). It can create a plot from functions or a data file in a manner very similar to the plot command. See plot (p. ) for features common to the plot (p. ) command; only differences are discussed in detail here.

Is gnuplot easy?

gnuplot is a not-quite-as-easy-to use, though extremely powerful, command-line plotting program. Running gnuplot is easy: from a command prompt on any system, type gnuplot.

How use gnuplot command in Linux?

Script files are simply ascii files that have commands written out just as you would enter them interactively. You can run a script two ways: Type load "scriptname" from within gnuplot. Or, from UNIX, run gnuplot by typing gnuplot scriptname .


1 Answers

Taking a quick look at the documentation for gnuplot 4.4 vs gnuplot 4.6 (current stable release), it appears that the feature you are trying to use was probably introduced in gnuplot 4.5 (Odd numbers are the development branches -- when they are deemed stable, they get incremented to an even number). The only way that can think of to accomplish this is to write a simple script in some other language which returns the column number (to stdout). Here's a simple example using python although I'm positive that you could do this in awk if you wanted to remain 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 doesn't do anything fancy (It assumes 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 as: python script.py datafile AGE to find out which column "AGE" is in. It is an error if "AGE" isn't in any column.

like image 197
mgilson Avatar answered Sep 16 '22 17:09

mgilson