Custom legend (or text) gnuplot

I have a file with 3 columns, the first 2 is the xy position, and the third I use it to determine 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: is it possible to have the right legend with such a point and COLOR ?. Since the dots will have different colors (according to the palette), I want to indicate what each color in the legend means.

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

So please help!

+6
source share
2 answers

There is no choice for this, you need to play a little. Here is YAGH (Another gnuplot hack);)

Assuming your values ​​are equally spaced, you can use the special name '+' with the build type labels .

To display only the user 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 (from 4.6.4):

enter image description here

Since set samples does not affect data graphs, you can integrate them directly into the 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 the correct xrange, yrange and the values key_x , key_y and key_dy .

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

+6
source

I have an alternative solution posted here: Using Gnuplot to graphically display dot colors

Essentially, you draw once without writing down the legend, then create dummy graphs (no data) for each dot color / label.

+1
source

Source: https://habr.com/ru/post/958929/


All Articles