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.
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?
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?
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.
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:
plot a=0 'data.txt' u 1:(a=a+$2,a) w lp
(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.plot a=0 'data.txt' u (a=a+$2,$1):2 w lp
y
. The string 'y'
interpreted as floating point number gives NaN
. a+NaN=NaN
. So, a
will get and stay NaN
.plot a=0 'data.txt' u ($1):(a=a+$2,a) w lp
($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.plot a=0 'data.txt' u ($1>1 && $1<4? $1:1/0):(a=$2+a,a) w lp
plot a=0 'data.txt' u ($1>1 && $1<4? $1:1/0):(a=$2+a,$2) w lp
$2
is interpreted as NaN
and NaN
is simply not plotted, but the rest of column 2 is plotted.plot a=0 'data.txt' u 1:($1>1 && $1<4? a=$2+a: 1/0,a) w lp
Possible solutions:
# x y
in your dataplot FILE u 1:2 skip 1
plot FILE u 1:2 ti columnhead