user1672190
user1672190

Reputation: 159

How to use gnuplot to do conditional plot

I am new to gnuplot and having a hard time to figure how to do conditional plot.

I have a data file, and I want to plot the data from 1st and 2nd columns to generate a line chart, save to JPEG/PNG file.

test.txt

1 9.9999 0
2 9.9999 0
3 9.9999 1
4 5.6000 0
5 3.4000 0
6 9.9999 1

What I want to do is plot 1st and 2nd column form this data file, when the value of 2nd column is 9.9999, then set the value to 0. That means whenever 9.9999 happens, it shows as 0 in the chart.

This is the code I am using, but seems doesn't work.

plot 'test.txt'  using 1:($2==9.99999?0:$2) with linespoint title 'test'

Upvotes: 0

Views: 296

Answers (2)

Tobias Müller
Tobias Müller

Reputation: 605

In addition to the incorrect number you're comparing with, it is usual problematic to compare floating point numbers for equality.

Think of something like

 plot 'test.txt'  using 1:(abs($2-9.9999)<0.1?0:$2) with linespoint title 'test'

Upvotes: 0

Yehia
Yehia

Reputation: 528

Your syntax is correct. (See 'Operators pp.28-30 of the gnuplot manual for more info.)

As @andyras mentioned, check that you have the correct value to compare to.

Upvotes: 0

Related Questions