'Getting the value of a specific element from a different row in gnuplot
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
Solution 1:[1]
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.
Solution 2:[2]
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.)
Solution 3:[3]
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)
Solution 4:[4]
ad a new column full of 13, then use:
plot 'datafile' using 1:($2/$3)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | otto.poellath |
| Solution 3 | Vikram Govindarajan |
| Solution 4 |
