How to plot points with label and color in Gnuplot?

Viewed 182

I want to plot points with individual labels and colors in Gnuplot.

I have a data file a.dat:

###label x y z  
1 244.8 18 6.1  
2 248.0 10.4 7  
3 294.4 6.3 13.7  
4 248.0 7.5 8.92  
5 240.0 3.69 6.61  
6 240.48 3.69 8.92  
7 256 5.7 15.8  
8 256 7 10.6  
9 256 4.1 8.2  
10 256 5.1 12.3  

The following commands work.

splot 'a.dat' using 2:3:4:1 with labels

set palette model RGB defined (0 'black',1 'blue', 2 'green', 3 'red')  
splot 'a.dat' using 2:3:4:($1==3?1:$1==6?2:$1==9?3:0) with points palette

But how can I mix them?

1 Answers

Assuming I understood your question correctly, do you really need a palette if you just want to set only a few specific colors to a few specific points? You are using two plotting styles with points and with labels you can combine them in one plot command.

Code:

### variable color points
reset session

$Data <<EOD
###label x y z
1 244.8 18 6.1
2 248.0 10.4 7
3 294.4 6.3 13.7
4 248.0 7.5 8.92
5 240.0 3.69 6.61
6 240.48 3.69 8.92
7 256 5.7 15.8
8 256 7 10.6
9 256 4.1 8.2
10 256 5.1 12.3
EOD

myColor(col) = column(col)==3 ? 0x0000ff : \
               column(col)==6 ? 0x00ff00 : \
               column(col)==9 ? 0xff0000 : 0

set key noautotitle
               
splot $Data u 2:3:4:(myColor(1)) w p pt 7 lc rgb var, \
         '' u 2:3:4:1 w labels offset 0.0,0.7,0.7
### end of code

Result:

enter image description here

Addition: (colored labels)

If you want to have colored labels then change the plot command as follows:

splot $Data u 2:3:4:1:(myColor(1)) w labels tc rgb var

Well, you have to decide:

  • using only labels it might be difficult to locate the exact position of your data point
  • using a point and a label without offset it might be diffcult to read the number

Result: (colored label without point)

enter image description here

Related