Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
811 views
in Technique[技术] by (71.8m points)

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....

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...