Search code examples
gnuplotnanconditional-operator

all points y value undefined and the syntax of "using" modifier


I came across an unexpected Gnuplot behaviour (Version 5.2 patchlevel 6) I could not explain, so far. This is the data (though any similar data would do):

x   y
0   1.1
1   1.0
2   1.3
3   1.4
4   0.9

The line

p a=0 'data.txt'  u 1:(a=a+$2,a) w lp

works perfectly: plots the progressive summation of column 2 and stores the partial sum in “a” (this is a simple, almost naive, way of integrating column 2).

pr a

returns 5.7, which is the expected value. But let us change the code a bit.

  1. Using an expression at left side of the colon.

The line

p a=0 'data.txt'  u (a=a+$2,$1):2 w lp

simply plots columns 1 and 2 and it works, but

pr a

Returns NaN. What is wrong in this case, since the summation process is nearly the same?

  1. Enclosing the left side of the colon in parenthesis.

This one

p a=0 'data.txt'  u ($1):(a=a+$2,a) w lp

returns “all points y value undefined!”. Why, since the syntax seems right?

  1. More complicated expressions

The origin of this problem was this line:

p a=0 'data.txt' u ($1>1 && $1<4? $1:1/0):(a=$2+a,a) w lp

which returns “all points y value undefined!” (I was trying to integrate column 2 in the xrange from 1 to 4). Nonetheless, the following works nicely (it is almost the same as before, but plots the column 2 instead "a")

p a=0 'data.txt' u ($1>1 && $1<4? $1:1/0):(a=$2+a,$2) w lp

However, “a” returns NaN again.

Finally, the following is the way to make it work perfectly, though it simply moves the ternary operator to the right side of the colon.

p a=0 'data.txt' u 1:($1>1 && $1<4? a=$2+a: 1/0,a) w lp

I am pretty sure all of this has a single cause, which I hope to be my poor understanding of the syntax, instead of a bug.

Any help would be appreciated.

Note: I have always used the abbreviated form of the commands. So I hope the code is clear enough.


Solution

  • I can confirm this behaviour not just for gnuplot 5.2.6 but also for newer versions.

    I guess there are a few things which might not be obvious and/or need to be known:

    • a + NaN = NaN and NaN + a = NaN
    • plot FILE u ($1):2 might behave differently in some cases compared to plot FILE u 1:2

    Let's have a look at your commands:

    1. plot a=0 'data.txt' u 1:(a=a+$2,a) w lp
    • works as expected, actually (a=a+$2) would be sufficient. The very first line will be ignored because for plotting, column 1 will be evaluated as NaN, hence not plotted and the rest of the line apparently will be ignored. Nothing will be added to a, i.e. still a=0. But the values of the following lines of column 2 will be added and give the expected result.
    1. plot a=0 'data.txt' u (a=a+$2,$1):2 w lp
    • first row, second column is y. The string 'y' interpreted as floating point number gives NaN. a+NaN=NaN. So, a will get and stay NaN.
    1. plot a=0 'data.txt' u ($1):(a=a+$2,a) w lp
    • my suspicion to explain the behaviour: ($1) is also interpreted as NaN and not plotted, but it seems the rest of the line is evaluated nevertheless. Hence, a + 'y' gets again NaN and a=NaN stays for the rest.
    1. plot a=0 'data.txt' u ($1>1 && $1<4? $1:1/0):(a=$2+a,a) w lp
    • same like 3.
    1. plot a=0 'data.txt' u ($1>1 && $1<4? $1:1/0):(a=$2+a,$2) w lp
    • for the very first line $2 is interpreted as NaN and NaN is simply not plotted, but the rest of column 2 is plotted.
    1. plot a=0 'data.txt' u 1:($1>1 && $1<4? a=$2+a: 1/0,a) w lp
    • same like 1.

    Possible solutions:

    1. remove or comment out your first line, i.e. # x y in your data
    2. or skip the first line plot FILE u 1:2 skip 1
    3. or use the first line as columnheader plot FILE u 1:2 ti columnhead