Search code examples
gnuplotlegend

Gnuplot legend with two boxes/groups and custom heading


I have two types of data/plot and I want to group them for clarification. For example, lets say I have this four data plots

set key box
pl x, x**2, sin(x), cos(x)

And the legend box comes out to be something like
enter image description here

But what I want is something like this, (with two box for each group and a custom heading for each group)

what_I_want

How do I do this? Is this type of legend possible with gnuplot?


Solution

  • I fear, that there is no bulit-in solution in Gnuplot for that. But you can try with multiplot:

    set key box
    set key width 3
    
    set multiplot
    
    set key at screen 0.45,0.95
    set key title "Polynomial"
    
    plot[-10:10][-20:100] x t 'x', x**2 t 'x^2'
    
    set key at screen 0.65,0.95
    set key title "Trigonometrical"
    
    plot[-10:10][-20:100] sin(x) lc 3, cos(x) lc 4
    
    unset multiplot
    

    Result

    EDIT

    As Eldrad wrote, you can fine tuning this solution by setting the margins and unsetting the tics and border of the second plot. Eg.:

    set key box
    set key width 3
    
    set multiplot
    
    set lmargin screen 0.05
    set rmargin screen 0.95
    set bmargin screen 0.05
    set tmargin screen 0.95
    set key at screen 0.45,0.95
    set key title "Polynomial"
    set tics
    set border
    
    plot[-10:10][-20:100] x t 'x', x**2 t 'x^2'
    
    set key at screen 0.65,0.95
    set key title "Trigonometrical"
    unset tics
    set border 0
    
    plot[-10:10][-20:100] sin(x) lc 3, cos(x) lc 4
    
    unset multiplot