Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gnuplot, how to label only certain points?

I'm using the following gnuplot commands to create a plot:

#!/bin/bash
gnuplot << 'EOF'
set term postscript portrait color enhanced
set output 'out.ps'

plot 'data_file' u 3:2 w points , '' u 3:2:($4!=-3.60 ? $1:'aaa') w labels

EOF

where data_file looks like this:

  O4     -1.20     -0.33     -5.20  
O9.5     -1.10     -0.30     -3.60  
  B0     -1.08     -0.30     -3.25  
B0.5     -1.00     -0.28     -2.60  
B1.5     -0.90     -0.25     -2.10  
B2.5     -0.80     -0.22     -1.50  
  B3     -0.69     -0.20     -1.10  

I want gnuplot to label all points with the strings found in column 1, except the one where column 4 is equal to -3.60 in which case I want the aaa string. What I'm getting is that the $4=-3.60 data point is being labeled correctly as aaa, but the rest are not being labeled at all.


Update: gnuplot has no problem showing numbers as labels using the conditional statement, ie: any column but 1 is correctly displayed as a label for each point respecting the conditions imposed. That is, this line displays column 2 (numbres) as point labels respecting the conditional statement:

plot 'data_file' u 3:2 w points , '' u 3:2:($4!=-3.60 ? $2:'aaa') w labels

Update 2: It also has no problem in plotting column 1 as point labels if I plot it as a whole, ie not using a conditional statement. That is, this line plots correctly all the point labels in column 1 (strings):

plot 'data_file' u 3:2 w points , '' u 3:2:1 w labels

So clearly the problem is in using the conditional statement together with the strings column. Any of these used separately works just fine.

like image 635
Gabriel Avatar asked Jun 27 '12 14:06

Gabriel


2 Answers

In a more clean way maybe, this should work. It seems label can't display a computed number if it isn't turned in a string.

#!/bin/bash
gnuplot << 'EOF'
set term postscript portrait color enhanced
set output 'out.ps'

plot 'data_file' u 3:2 w points , '' u 3:2:($4!=-3.60 ? sprintf("%d",$1):'aaa') w labels

EOF
like image 142
T. Verron Avatar answered Nov 05 '22 17:11

T. Verron


Is this what you want?

#!/bin/bash

gnuplot << 'EOF'
set term postscript portrait color enhanced
set output 'out.ps'
plot 'data_file' u 3:2 w points , \
     '' u (($4 == -3.60)? 1/0 : $3):2:1 w labels

EOF

All I do here is set (x) points where the column 4 equals -3.6 to NaN (1/0). Since gnuplot ignores those points, life is good. I think the problem with your script is that you were filtering a column where gnuplot expects string input -- although I haven't played around with it enough to verify that. I just switched the filter to a column where gnuplot expects numbers (the x position) and it works just fine.

like image 21
mgilson Avatar answered Nov 05 '22 16:11

mgilson