user1836103
user1836103

Reputation: 11

Gnuplot: How to use multiple values from one data column?

I've got a multi collumn data file. Let's say I need to only plot the 4th number from the 13th line. This works well with the following piece of code:

plot 'datafile' u (some fix x-value):($0==13? $4 :1/0) with points

Now I would like to plot the average of these numbers from the 13th and from the 11th line. Something like this:

plot 'datafile' u (some fix x-value):( ($4(line11)+$4(line13.))/2 ) with points

As far as I know theres no way to address both lines in gnuplot, right? Can I use awk or sed in gnuplot to do it? Maybe to store value from line 11 in a variable that can be used in a function of line 13?

Thank you very much in advance for your kind help!

Best wishes, Tob

Upvotes: 1

Views: 1581

Answers (2)

theozh
theozh

Reputation: 25988

Of course, you can also do something with gnuplot only.

Option 1: Extract the values of interest via stats and every.

  • stats calculates some statistical values min,max,mean, etc. from one or two columns (check help stats)
  • every limits the range to certain row or block numbers, here: to a single row, i.e. the row of interest (check help every)
  • hence, in this limited case min, max and mean will all be the same and equal to the value of interest

Option 2: Remember the values within the actual plot command with help of ternary operator and serial evaluation (check help ternary, help operators binary)

  • initialize the values v1 and v2 toNaN.
  • if you get to the rows of interest ($0==row1-1 and $0==row2-1), assign the values to v1 and v2 (check help pseudocolumns)
  • only if v1 and v2 are both different from NaN do your calculation and plot this value. Check gnuplot: How to compare to NaN?

In the following example, from column 4 the average of the values in row 11 (here: 11.4) and row 13 (here: 13.4) should be plotted, which is 12.4.

Keep in mind that line/row numbering in gnuplot starts from 0.

This script can easily be adapted to other variations.

Data: SO13456636.dat

 1.1    1.2    1.3    1.4
 2.1    2.2    2.3    2.4
 3.1    3.2    3.3    3.4
 4.1    4.2    4.3    4.4
 5.1    5.2    5.3    5.4
 6.1    6.2    6.3    6.4
 7.1    7.2    7.3    7.4
 8.1    8.2    8.3    8.4
 9.1    9.2    9.3    9.4
10.1   10.2   10.3   10.4
11.1   11.2   11.3   11.4
12.1   12.2   12.3   12.4
13.1   13.2   13.3   13.4
14.1   14.2   14.3   14.4

Script: (works with gnuplot >=4.6.0, March 2012)

### do some math with two values from the same row
reset

FILE = "SO13456636.dat"

myCol  = 4
myRow1 = 11
myRow2 = 13

set grid x,y
set key noautotitle

set multiplot layout 2,1

    # Option 1
    stats FILE u myCol every ::myRow1-1::myRow1-1 name "v1" nooutput
    stats FILE u myCol every ::myRow2-1::myRow2-1 name "v2" nooutput
    plot  FILE u (1):((v1_min+v2_min)*0.5) w p pt 7 lc rgb "red"

    # Option 2
    plot v1=v2=NaN FILE u (1):($0==myRow1-1 ? v1=column(myCol) : 0, \
                               $0==myRow2-1 ? v2=column(myCol) : 0, \
                          (v1==v1 && v2==v2 ? (v1+v2)*0.5 : NaN)) w p pt 7 lc rgb "blue"
unset multiplot
### end of script

Result:

enter image description here

Upvotes: 0

Ed Morton
Ed Morton

Reputation: 203924

I haven't used gnuplot in a very, VERY long time and all I really remember about it is that I used to rely pretty heavily on awk to parse the data files. To get the 4th "number" (I assume you mean field/column) from the 13th line in awk would just be:

awk 'NR==13{print $4}' datafile

and to get the averages of the values from the 4th field in the 11th and 13th lines would be:

awk 'NR~/^(11|13)$/{sum+=$4} END{print sum/2}' datafile

Now if someone can tell you how to use awk's output in gnuplot (sorry, I just don't remember) then you're in business.

EDIT: a quick google of gnuplot and awk showed up this:

gnuplot> plot "<awk '{x=x+$2; print $1,x}' file1.dat" with boxes

from http://security.riit.tsinghua.edu.cn/~bhyang/ref/gnuplot/datafile3-e.html plus several other examples. HTH.

Upvotes: 1

Related Questions