mahmood
mahmood

Reputation: 24705

gnuplot conditional plotting with if

I have a data file with two columns

10  0.5
20  0.8
25  0.3
15  0.6

I want to plot the second column if the first column is less than or equal 20. Problem is, I want to skip the rows where the first column is greater than 20, however gnuplot forces me to do something in the conditional part.

The command is

plot 'data.txt' u ($1<=20?$2:0) with points

As you can see, I have to specify to put a point of ZERO. I don't want that! I want to skip....

Upvotes: 1

Views: 10488

Answers (1)

Christoph
Christoph

Reputation: 48390

To skip a point in gnuplot you must give it an invalid value like 1/0:

plot 'data.txt' u 1:($1 <= 20 ? $2 : 1/0) with points

For some plotting styles the presence of invalid values deserves some attention. If the remaining points should be connected e.g. with lines, the line is interrupted at an invalid point.

Since gnuplot version 5.0.6 one can use set datafile missing NaN to treat invalid points like missing ones. The filtered data then behaves as if the invalid points don't exist. See https://stackoverflow.com/a/46070360/2604213 for a working example.

Upvotes: 6

Related Questions