kabanus
kabanus

Reputation: 26005

Separate key (legend) for colors and markers

I have a plot with several types of objects (each read from a separate file). I'm plotting the same several functions for all of them, all on the same graph (same X-axis).

I set the markers (pt) explicitly for each, and the color (lc), so the same object has the same marker, but the same function has the same color. As an example we have 2 files, one for each object (| is just to separate the files here):

0 0 0   |   0 1 1
1 1 2   |   1 1 2

Let's call the left file A, the right B. Column 1 in each file is the x axis, column 2 is using 1:2, and column 3 is using 1:3. So using the above files in an interactive session:

gnuplot> plot "A" using 1:2 with lp pt 1 lc 'black'
gnuplot> replot "A" using 1:3 with lp pt 1 lc 'red'
gnuplot> replot "B" using 1:2 with lp pt 2 lc 'black'
gnuplot> replot "B" using 1:3 with lp pt 2 lc 'red'

we get:

theplot

Is it possible to have the key separated, so A/B appear next to their respective marker, and the function name ("using...") appears next to a line (or anything) with the appropriate color?

Right now by omitting titles (notitle in the plot command) I can get one or the other, though I would have to settle on some uniform arbitrary marker/color (depending on what I chose to set as key). Can I:

  1. Get two keys somehow? - Preferably setting the missing attribute (color or marker) to something not in the plot.
  2. If not, can I customize a manual legend somehow?

Upvotes: 1

Views: 1718

Answers (1)

ewcz
ewcz

Reputation: 13097

I am not fully sure what you want to achieve, nevertheless as for the splitting of the key, I don't think that Gnuplot has some "out-of-the-box" feature for this. However, you could (ab)use multiplot to achieve this effect. The idea is basically to generate two overlapping plots - one with points and one with lines - and to position the keys independently:

set terminal pngcairo rounded font ",16"
set output 'fig.png'

$A << EOD
0   0   0
1   1   2
EOD
$B << EOD
0   1   1
1   1   2
EOD

set multiplot

set xtics out nomirror
set ytics out nomirror

eps = 0.1
set lmargin at screen eps
set rmargin at screen 1 - eps/2
set bmargin at screen eps
set tmargin at screen 1 - eps/2

#common key settings
set key left top Left reverse spacing 1.5

set key at screen 0.1,screen 1-eps
plot \
    $A u 1:2 with p ps 1.5 pt 1 lc 'black' t 'A', \
    $A u 1:3 with p ps 1.5 pt 1 lc 'red' t 'A' , \
    $B u 1:2 with p ps 1.5 pt 2 lc 'black' t 'B', \
    $B u 1:3 with p ps 1.5 pt 2 lc 'red' t 'B'

unset border; unset xtics; unset ytics

set key at screen 0.3,screen 1-eps
plot \
    $A u 1:2 with l lc 'black' t 'using 1:2', \
    $A u 1:3 with l lc 'red' t 'using 1:3', \
    $B u 1:2 with l lc 'black' t '', \
    $B u 1:3 with l lc 'red' t ''

This would give you: enter image description here

Upvotes: 2

Related Questions