Friday, September 9, 2011

Manipulate data using ternary operator in gunplot

In gnuplot there is an operator "? :" which is ternary. It behaves as it does in C language. For example, "a?b:c" means if a is true then return 'b', otherwise return 'c'. Using this operator we can do some complex operation on input data. Let us see an example.

We have a data file as follows:
#x  y1   y2
0.1	0.2	0.2
0.2	0.4	0.4
0.3	0.6	0.6
0.4	0.8	0.8
0.5	1	1
0.6	1.2	1.2
0.7	1.4	1.4
0.8	2.4	0.6
0.9	2.7	0.675
1	3	0.75
1.1	3.3	0.825
1.2	3.6	0.9
1.3	3.9	0.975
1.4	4.2	1.05
1.5	4.5	1.125
We want to plot the points y1=y2 with blue pints, and other points y1!=y2 (y1 not equals y2) with some other colors. To realize this goal, we can use ternary operator. According to the definition of ternary operator, expression "$2==$3?$2:1/0" will return the points satisfy $2=$3, and when $2!=$3 it returns undefied value(1/0 is an undefied value). So when we use command "plot 'data.dat' u 1:($2==$3?$2:1/0) w p", we plot the points with y1=y2. In a simillar manner, we can plot the points with y1!=y2. Now we come to our final plotting script.
reset
set term png font "Times,20"
set output "ternaty.png"
set xlabel "x"
set ylabel "y"
plot "data.dat" u 1:($2==$3?$2:1/0) w p lc rgb"blue" notitle,\
     "data.dat" u 1:($2!=$3?$2:1/0) w p lc rgb"green" notitle,\
     "data.dat" u 1:($2!=$3?$3:1/0) w p lc rgb"red" notitle
The picture comes out to be like follows.

Manipulate data using ternary operator in gnuplot

3 comments:

  1. IOT Training in Bangalore - Live Online & Classroom
    Iot Training course observes iot as the platform for networking of different devices on the internet and their inter related communication. Iot Training in Bangalore

    ReplyDelete
  2. Gaining Python certifications will validate your skills and advance your career.
    python certification

    ReplyDelete

Creative Commons License
Except as otherwise noted, the content of this page is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.