Search code examples
gnuplot

accessing the nth datapoint in a datafile using gnuplot


After searching through all pipes to and inbuilt functions in gnuplot, I still haven't found a simple way to access the nth element of my datafile (not through increment, the single value) for use as a constant in a function (or parameter in a for loop) for my plots. For example I have a monotonic decreasing datafile with two columns and I want to normalize the y values while plotting so the y ranges from one (1) to zero (0) by subtracting the last value from each datapoint and dividing the subtraction by the difference between the first datapoint and the last. I tried awk but I'm not too familiar with the syntax. If there is a simple way I would love to know.

For example

plot "my2columndata.dat" using 1:(($2-'lastdatapoint')/('firstdatapoint'-'lastdatapoint'))

or something of the sorts where first and last datapoints are eponymous - they are the first and last datapoints in the monotonic decreasing datafile "my2columndata.dat"


Solution

  • In your case (since you know that the datafile is monotonically decreasing), this isn't too hard to do.

    set terminal unknown
    plot 'my2columndata.dat' # gather basic statistics
    first=GPVAL_DATA_Y_MAX
    last=GPVAL_DATA_Y_MIN
    
    set terminal <whatever>
    set output <whatever.wht>
    plot 'my2columndata.dat' u 1:(($2-last)/(first-last))
    

    This method gathers information about the datafile without creating an output, then replots using the GPVAL variables. (You can see this in gnuplot after plotting with the command show variables all.) In gnuplot 4.6.0 and above you can use the stats command to gather data without plotting as I have done here; stats creates different variable names though.

    Accessing the n-th data point (as your question title hinted) would be trickier to do.