0
votes

Let's say my gnuplot script wants to put a label or an arrow on a specific point in the data. The data have been created in another program and it could encodne them into the header line of the data file. Unfortunately gnuplot fails to use columnheader(num) to pull the data out.

Example. I create histogram data including mean, sigma and some limit values. The program writing the data file puts that into the first line. Looks like this: (0.2743 is the mean and 3.0 is an outlier limit)

"Data" 0.2743 1.0
     0 "-INF ->  -5.0"      0   0.00
     1 " -5.0 ->  -4.5"      0   0.00
     2 " -4.5 ->  -4.0"      2   0.03
     3 " -4.0 ->  -3.5"      4   0.06
     4 " -3.5 ->  -3.0"      3   0.05
     5 " -3.0 ->  -2.5"      5   0.08
     6 " -2.5 ->  -2.0"     19   0.30
     7 " -2.0 ->  -1.5"     49   0.78
     8 " -1.5 ->  -1.0"    193   3.07
     9 " -1.0 ->  -0.5"    527   8.39
    10 " -0.5 ->  +0.0"   1289  20.53
    11 " +0.0 ->  +0.5"   1878  29.90
    12 " +0.5 ->  +1.0"   1411  22.47
    13 " +1.0 ->  +1.5"    636  10.13
    14 " +1.5 ->  +2.0"    178   2.83
    15 " +2.0 ->  +2.5"     56   0.89
    16 " +2.5 ->  +3.0"     17   0.27
    17 " +3.0 ->  +3.5"      9   0.14
    18 " +3.5 ->  +4.0"      4   0.06
    19 " +4.0 ->  +4.5"      0   0.00
    20 " +4.5 ->  +5.0"      0   0.00
    21 " +5.0 -> +INF"      0   0.00

Now I tried to reference that from the script to create arrows in the plot.

...
mean= columnheader(2)
sdev= columnheader(3)
lboffs= 0.2
set arrow  from 11.0 + mean,0.0 to 11.0 + mean ,GPVAL_DATA_Y_MAX*1.2 nohead lw 2 lc rgb "dark-green"
set arrow  from 11.0 + mean - 3 * sdev,0.0 to 11.0 + mean - 3 * sdev ,GPVAL_DATA_Y_MAX*1.2 nohead lw 2 lc rgb "red"
set arrow  from 11.0 + mean + 3 * sdev,0.0 to 11.0 + mean + 3 * sdev ,GPVAL_DATA_Y_MAX*1.2 nohead lw 2 lc rgb "red"
set arrow  from 11.0,0.0 to 11.0,GPVAL_DATA_Y_MAX*1.2 nohead lw 2 lc rgb "blue"
set label "Mean" at 11.0 + mean + lboffs,GPVAL_DATA_Y_MAX*1.1 tc rgb "dark-green"
set label "+3%"  at 11.0 + mean + 3 * sdev + lboffs,GPVAL_DATA_Y_MAX*1.1 tc rgb "red"
set label "-3%"  at 11.0 + mean - 3 * sdev + lboffs,GPVAL_DATA_Y_MAX*1.1 tc rgb "red"
...

Turns out that doesn't work.

gnuplot> mean= columnheader(2)
         undefined function: columnheader

gnuplot> sdev= columnheader(3)
         undefined function: columnheader

What's missing? Or is there a better way to feed variables into gnuplot?

Thanks, Gert

1

1 Answers

0
votes

The function columnheader does not work outside of a plot context. However, there are several options in order to access the values in the first row of your data file.

For example, you could use external processing:

fName = 'input.dat'

getColumn(n)=system(sprintf("head -n1 %s | awk '{print $%d}'", fName, n))*1.
mean = getColumn(2)
std = getColumn(3)

Alternatively, you might use the stats command in combination with every in order to select just the very first line:

stats fName every ::0::0 using 2:3 nooutput
mean = STATS_min_x
std = STATS_min_y