Search code examples
gnuplot

gnuplot set two keys side by side and share a title?


This is my figure: tu1 I want to change the keys to this: lin Below is my code, what should I do?

set tmargin at screen 0.995;    # top and right usually close to 1
set rmargin at screen 0.995;
set bmargin at screen 0.05;
set lmargin at screen 0.05;

set xr[0:0.24]
set yr[0:0.24]
set xtics border offset   0,0.4 0,0.05,0.2
set ytics border offset   0.8,0 0,0.05,0.2
set xlabel 'RHS' offset 0,3
set ylabel 'LHS' offset 9,0

set key at 0.22,0.1

plot '-' w p ps 2 pt 11 lc 8 title "$\kappa$",\
     '-' w p ps 2 pt 7 lc 8,\
     '-' w p ps 2 pt 9 lc 8,\
     '-' w p ps 2 pt 13 lc 8,\
     '-' w p ps 2 pt 10 lc 8,\
     '-' w p ps 2 pt 6 lc 8,\
     '-' w p ps 2 pt 8 lc 8,\
     '-' w p ps 2 pt 12 lc 8,\
     x dashtype 2 lc 8
0.208555 0.204212
e
0.108604 0.107899
e
0.055720 0.055651
e
0.037514 0.037498
e
0.208555 0.230125
e
0.108604 0.115062
e
0.055720 0.057531
e
0.037514 0.038354
e

I didn't find a legend on how to do this juxtaposition in the website, hope someone can teach me.


Solution

  • There is the key option maxrows (check help key). With this you can limit the number of rows in the legend, however, depending on whether you need a title for x you may need to play other tricks. However, the horizontal space between the columns is rather large. You could reduce the space for the symbol (actually line) in the legend via samplen 0 and use the option reverse, this would place the titles between the symbols, unfortunately not centered. However, this still doesn't look too good, and even worse if your titles do not have the same length.

    If you want to get the symbols closer, you could also use multiplot (check help multiplot) to overlay shifted legends, but I would call this rather cumbersome.

    I minimized your script a little.

    Script:

    ### shared title in legend
    reset session
    
    $Data <<EOD
    0.208555   0.204212
    
    0.108604   0.107899
    
    0.055720   0.055651
    
    0.037514   0.037498
    
    0.208555   0.230125
    
    0.108604   0.115062
    
    0.055720   0.057531
    
    0.037514   0.038354
    EOD
    
    set xr[0:0.24]
    set yr[0:0.24]
    set xlabel 'RHS'
    set ylabel 'LHS'
    
    set key at graph 0.9, 0.4
    set key maxrows 4 title "Legend title" offset -3,0.2 font ",13"
    set key samplen 0 reverse
    
    myPts      = "11 7 9 13 10 6 8 12"
    myPt(i)    = int(word(myPts,i))
    myTitles   = "title1 title2 title3 title4"
    myTitle(i) = i>words(myTitles) ? ' ' : word(myTitles,i)."  "
    
    plot for [i=1:8] $Data u 1:2 every :::i-1::i-1 w p ps 2 pt myPt(i) lc 8 ti myTitle(i), \
         x w l dt 2 lc 8 notitle
    ### end of script
    

    Result:

    enter image description here

    Addition:

    Here would be the multiplot solution. Since you are using fixed margins in your original script anyway, it is not too much extra effort. You need to play with the graph coordinates of the second key to get the desired placement.

    Script:

    ### shared title in legend using multiplot
    reset session
    
    $Data <<EOD
    0.208555   0.204212
    
    0.108604   0.107899
    
    0.055720   0.055651
    
    0.037514   0.037498
    
    0.208555   0.230125
    
    0.108604   0.115062
    
    0.055720   0.057531
    
    0.037514   0.038354
    EOD
    
    set xr[0:0.24]
    set yr[0:0.24]
    set xlabel 'RHS'
    set ylabel 'LHS'
    
    set margin 10,5,4,2
    
    set key at graph 0.9, 0.4
    set key title "Legend title" offset -3,0.2 font ",13"
    set key samplen 0 reverse
    
    myPts      = "11 7 9 13 10 6 8 12"
    myPt(i)    = int(word(myPts,i))
    myTitles   = "title1 title2 title3 title4"
    myTitle(i) = i>words(myTitles) ? ' ' : ' '.word(myTitles,i)
    
    set multiplot
    
        plot for [i=1:4] $Data u 1:2 every :::i-1::i-1 w p ps 2 pt myPt(i) lc 8 ti myTitle(i), \
            x w l dt 2 lc 8 notitle
    
        set key at graph 0.8,0.4 title " "
        unset border
        unset tics
        unset xlabel
        unset ylabel
        plot for [i=5:8] $Data u 1:2 ever :::i-1::i-1 w p ps 2 pt myPt(i) lc 8 ti " "
    
    unset multiplot
    ### end of script
    

    Result:

    enter image description here