1

I have 10x10x10 grid points. Some of these points are associated with a value 1 and the others are associated with a value -1. I want to specify(give a color to) only those points which have value 1. Can anyone please tell me how this can be achieved in Gnuplot.

Thanks in advance.

  • An obvious way to do this is by filtering your data with some utility. If your data structure is `x y z w` where w is either 1 or -1, then you can use `awk` to filter this data to a temporary file which will only display the data points with w=1: `awk '{if ($4 == 1) print $0}' datafile > temp` and then plot it with gnuplot: `splot "temp"`. – Miguel Apr 12 '14 at 23:12
  • @Miguel If the OP wants only to draw points, the filtering inside of gnuplot is also possible: `splot 'file' using 1:2:($4 == 1 ? $3 : 1/0) with points`. – Christoph Apr 13 '14 at 09:47
  • Thanks Miguel and Christoph. @ Christoph: I am a newbie to gnuplot, so can you also please take a little more trouble to explain me what the syntax means. Thanks – naniiitb777 Apr 13 '14 at 10:09
  • @Christoph: I didn't know about this `1/0` thing, but it seems pretty useful. – Miguel Apr 13 '14 at 11:14
  • @Miguel That trick works fine as long as you plot points. If you want to filter out some points but connect the others with lines, it doesn't work: http://stackoverflow.com/a/19001406/2604213 – Christoph Apr 13 '14 at 12:09

1 Answers1

0

If you want to filter out completely all points with value -1, you can do it as follows:

splot 'file' using 1:2:($4 == 1 ? $3 : 1/0) with points

That assumes, that your data file has four columns with the x, y, z value in the columns 1, 2, 3 and in the fourth column the values 1 or -1.

With the using statement you can specify which columns are used for plotting: using 1:2:3 uses the first column as x, the second as y and the third column as z value.

You can also do calculations inside the using statement. In that case you must put the respective expression in braces, and refer to the values of a column with e.g. $3 or column(3): using 1:2:($3/10) would scale the values in the third column by 10 and use the result as z value.

The expression I used above, using 1:2:($4 == 1 ? $3 : 1/0) does the following: If the value in the fourth column is equal to 1, use the value in the third column, otherwise use 1/0. The 'special' value 1/0 makes gnuplot ignore a point.

Christoph
  • 47,569
  • 8
  • 87
  • 187