Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom legend (or text) gnuplot

I have a file with 3 columns, the first 2 are the position x y and the 3rd one I use it for define the color so I have something like this:

set palette model RGB defined ( 1 'black', 2 'blue', 3 'green', 4 'red')
unset colorbox

plot "file" u 2:1:3 w points pt 14 ps 2 palette, "file2" u 2:1:3 w points pt 14 ps 2        palette 

Now the question: Is it possible to have a proper legend with this kind of point and COLOR?. Since the points will have different colors (according to the pallete) I want to specify what means each color in the legend.

The only solution I was thinking was to write somewhere in the plot some text with the character of the point (in this case pt 14) and specify the color... but is not really a solution right?

So please help!

like image 922
Nikko Avatar asked Nov 27 '13 14:11

Nikko


2 Answers

There is no option for this, you need to fiddle a bit. Here is YAGH (Yet another gnuplot hack) ;)

Assuming that your values are equidistantly spaced, you can use the '+' special filename with the labels plotting style.

To show only the custom key, consider the following example:

labels="first second third fourth"
set xrange[0:1] # must be set for '+'
set yrange[0:1]
set samples words(labels)   # number of colors to use
key_x = 0.8     # x-value of the points, must be given in units of the x-axis
key_y = 0.8
key_dy = 0.05
set palette model RGB defined ( 1 'black', 2 'blue', 3 'green', 4 'red')
unset colorbox
plot '+' using (key_x):(key_y + $0*key_dy):(word(labels, int($0+1))):0 \
    with labels left offset 1,-0.1 point pt 7 palette t ''

This gives (with 4.6.4):

enter image description here

As the set samples doesn't affect the data plots, you can integrate this directly in your plot command:

...
unset key
plot "file" u 2:1:3 w points pt 14 ps 2 palette, \
     "file2" u 2:1:3 w points pt 14 ps 2 palette, \
     '+' using (key_x):(key_y - $0*key_dy):(word(labels, int($0+1))):0 \
         with labels left offset 1,-0.1 point pt 14 ps 2 palette

But you need to set a proper xrange, yrange and the values of key_x, key_y and key_dy.

This is not the most intuitive way, but it works :)

like image 90
Christoph Avatar answered Sep 30 '22 13:09

Christoph


I have an alternative solution posted here: Using Gnuplot to plot point colors conditionally

Essentially you plot once without a legend entry, then make dummy plots (with no data) for each point color/label.

like image 36
andyras Avatar answered Sep 30 '22 13:09

andyras