35

How can I do this in gnuplot:

plot "test.csv" using 1:2 if value_in_column_3 == 80.0

It should only select those rows where column 3 == 80.0 and ignore all other rows (It should not plot a 0 for the other rows, simply ignore them)

Thanks in advance.

Korizon
  • 3,677
  • 7
  • 37
  • 52

5 Answers5

48

Consider the following dataset (1.dat),

1 0.8 0
2 0.6 0
3 0.9 1
4 1.1 0
5 0.7 0
6 0.6 1

where we want to plot the first two columns only when the third one equals zero. Then you can try this:

plot '1.dat' using 1:($3==0?$2:1/0)

(Credit to markjoe on Gnuplot mailing-list.)

chl
  • 27,771
  • 5
  • 51
  • 71
  • 7
    Note that since `1/0` is an invalid value, this will cause problems if one chooses to plot `lines` or `linespoints`; this is discussed in http://stackoverflow.com/questions/11187691/gnuplot-conditional-plotting-2-15-2-1-0-with-lines – sdaau Jul 10 '13 at 21:27
  • 1
    How does this work? I get it except for the 1/0 part. – SDsolar May 24 '18 at 17:32
  • @sdaau, there is a simple gnuplot-native workaround. Check this: https://stackoverflow.com/a/54475591/7295599 – theozh Feb 01 '19 at 16:52
  • 3
    Using `NaN` in place of `1/0` is better. – Mead Oct 15 '20 at 13:45
21

Case where one wants to plot conditionally on another column containing text:

data

1 0.8 a
2 0.6 a
3 0.9 a
1 2.1 b
2 1.7 b
3 1.6 b

code

set terminal postscript color
set xrange [0:4]
set yrange [0:3]
plot "1.dat" using 1:(stringcolumn(3) eq "a"? $2:1/0) title "a" lc rgb "blue" ,\
  "" using 1:(stringcolumn(3) eq "b"? $2:1/0) title "b" lc rgb "red"

command

gnuplot < 1.par > 1.ps
tflutre
  • 3,354
  • 9
  • 39
  • 53
7

As chl says above, the only way to do this in gnuplot is rather hacky: you have to use gnuplot's terniary ?: operator to generate a numerical error on the points you want to filter out of your dataset.

I may be biased here as I'm an author on the project, but you may want to have a look at Pyxplot http://www.pyxplot.org.uk (also free and open source), written by a group of gnuplot users who were a bit fed up with hacky syntax like this.

Its syntax is very similar to gnuplot, but with extensions. For what you want, you can specify a "select criterion" in the plot command, and points are only included if it tests True. See http://pyxplot.org.uk/current/doc/html/sec-select_modifier.html for more information.

Dominic Ford
  • 224
  • 3
  • 2
  • 1
    Rather than using 1/0, you can just use NaN (Not a Number), which has exactly the same effect but seems more aesthetically pleasing. – Lee Phillips Dec 04 '13 at 18:06
7

Another hack would be to use a shell command like awk:

plot "< awk '$3==80.0 { print $1, $2 }' test.csv" using 1:2
Kevin Brown-Silva
  • 40,873
  • 40
  • 203
  • 237
user2817215
  • 71
  • 1
  • 1
5

If you are calling a script, use column(2) instead of $2

plot "1.dat" using 1:(stringcolumn(3) eq "a"? column(2):1/0) title "a" lc rgb "blue" ,\
  "" using 1:(stringcolumn(3) eq "b"? column(2):1/0) title "b" lc rgb "red"
EverythingRightPlace
  • 1,197
  • 12
  • 33
Dina Said
  • 51
  • 1
  • 1