6

Using gnuplot 4.2, is it possible to obtain the value of a specific column/row and use that value somehow?

For example, let's say my datafile contains the following

#1  2
7  13
5  11
23 17
53 12

For a simple plot where column 1 is the x axis and column 2 is the y axis I would:-

plot 'datafile' using 1:2

What I'm trying to do is to normalize the all data in column 2 by the first element in that column (13). Is there a way to do this in gnuplot itself (i.e., without resorting to a scripting language or something to preprocess the data first)?

Cheers

4 Answers 4

7

Using the running averages demo, I managed to achieve a plot normalized to the first value of the second column.

The base variable is used to store the reference value, and the first function initializes base on the first row.

first(x) = ($0 > 0 ? base : base = x)
plot file.dat u 1:(first($2), base/$2)

It should be mentioned that this was not done using gnuplot 4.2.

Edit: Updated using Christoph's advice.

Sign up to request clarification or add additional context in comments.

2 Comments

Nice, +1. To be more general one could check for the row number being 0 for setting the base value: first(x) = ($0 > 0 ? base : base = x).
This solution assumes, however, that the file does not contain single or double empty lines. If you have empty lines you will have the condition $0==0 several times.
1

If your base value (e.g. 13) is in the first row of your data set, you should be able to do what you want using the CVS version of gnuplot.

Have a look at the running averages demo. Along those lines, you could write a custom function that stores the base value in a custom variable when called for the first time, and returns that variable on subsequent calls.

(Since that demo is listed for the CVS version only, I assume the required functionality is not available in the current release version.)

1 Comment

Hmmm, the running averages demo code has gone 404. :( In version 5, it looks like this: skuld.bmsc.washington.edu/~merritt/gnuplot/demo_canvas/…
1

You can use the stats command to get the first value of the second column and store it in the reference variable as shown below,

stats 'datafile' using (reference=$2) every ::0::0 nooutput

You can replace $2 with any other column you want. The zeros in every ::0::0 implies the first entry of the column and you can also use every ::n::n to get the nth entry of the column. nooutput is used to avoid the cluttered console.

You can then get normalized plot using,

plot 'datafile' using 1:($2/reference)

1 Comment

Just for completeness: in case you might have single or double empty lines in your data you'd better use: stats 'datafile' u (reference=$2) index 0 every ::0:0:0:0 nooutput, otherwise you might get unexpected results.
-1

ad a new column full of 13, then use:

plot 'datafile' using 1:($2/$3)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.