Search code examples
colorsgnuplotlegend

GNUPLOT: Merge key entries with multiplot


I would like to plot several data colmuns of a datfile in one graph. For each data columnI would like to use a black (differently dashed) line and a coloured point. I found out how to do it in general (by plotting first the line (with lines) and then the points (with points) and afterwards shifting the legend entries on top of each other). This is explained for example in this post: Merge key entries in gnuplot

But it is not fully working in my case. I have three problems:

First: I would like to have a box around the legend. But this doesnot work when I shift the legend entries on top of each other...

Second: I would like to include a rectangle object. Somehow this always is on top of the plotted lines except the last one...

And the third problem: The xticlabels are plotted for each plot on each other. That is why they seem to be bold which they should not. I found out that I should "hide" the tics (like I do with the border and the labels) but it doesnot work for the tics somehow...

Do you have some hints for me? Best regards, Sebastian @ewcz

#dataset.dat
"\\footnotesize r/R"    "\\footnotesize OP1"    "\\footnotesize OP2"
0.132   1.018   0.872
0.162   0.940   0.796
0.191   1.014   0.848
0.221   1.043   0.934
0.250   1.010   0.935
0.279   0.987   0.938
0.309   0.962   0.930
0.338   0.929   0.921
0.368   0.897   0.922
0.397   0.876   0.932
0.426   0.831   0.919
0.456   0.795   0.884

#Start terminal
set terminal epslatex size 7.8cm, 6.1cm font ",10" 
#Legend settings
pointSize = 1
yticsScale =1
keySpacing = pointSize*yticsScale*1.25
keyY = 15.5
keyX = 0.975
set key vertical Left reverse width -0.5 height +0 font ",16"
set key opaque
set key autotitle columnheader
set key bottom right spacing -1
#Hide border & labels
set border 0
set xlabel " "
set ylabel " "
#Format of axis numbers
set format xy '$\%g$'
set format x '\footnotesize \%10.1f'
set format y '\footnotesize \%10.1f'
#Format tics
set xtics 0,0.1 out nomirror
set xtics offset -0.2,0
set mxtics 5
set ytics 0.6,0.1 out nomirror
set ytics offset 0.4,0
set mytics 5
#Background grid setting
set grid
show grid
set object 1 rectangle from 0.132, graph 0 to 0.456, graph 1 fillcolor rgb "#A9A9A9" fs pattern 1 noborder behind
#Margins
set lmargin 5.9
set rmargin 0.5
set bmargin 3.5
#Axis range settings
set xrange [0:0.535]
set yrange [0.6:1.2]
#Format lines, boxes...
set style line 4 lt 1 lc rgb 'black' lw 2 pt 13 ps 1.25 dt 4
set style line 5 lt 1 lc rgb 'black' lw 2 pt 4 ps 1.0 dt 5
#Multiplot
set multiplot
set origin 0,0
set size 1,1
#Plots
set key at graph keyX, character keyY
plot 'dataset.dat' using 1:2 with lines ls 4, \
'dataset.dat' using 1:2 with points ls 4 lc rgb "#71da71" title " "
#Label settings
set border
set xlabel '\small $r/D_T\;[-]$' offset 0,+0
set ylabel '\small $c_{m2} \cdot A_{T}/Q_T\;[-]$' offset +10.5,+0
#Last Plot
keyY = keyY - keySpacing
set key at graph keyX, character keyY
plot 'dataset.dat' using 1:3 with lines ls 5, \
'dataset.dat' using 1:3 with points ls 5 lc rgb "#4da6ff" title " "
#End of code
unset multiplot

Solution

  • I would propose the following:

    1. Since the keys in both plots are independent, perhaps the most straightforward solution would be to draw the encompassing box manually (see below) by using set object rectangle (although this might need some manual "tweaking" of the size of the box).
    2. The rectangle is on top of the plotted lines since it is duplicated by the second plot. In a sense, this second copy is behind with respect to the second plot, but since this layer is on top of the first plot, it covers the elements plotted by the first plot. One can get rid of this by deleting the object in the context of the second plot with unset object 1.
    3. It is a similar issue with the tics,labels,etc. In the code below, all the definitions are moved before the first plot command and then unset with respect to the second plot.

    With these modifications the script would look like:

    #Start terminal
    set terminal epslatex size 7.8cm, 6.1cm font ",10"
    
    #Legend settings
    pointSize = 1
    yticsScale =1
    keySpacing = pointSize*yticsScale*1.25
    keyY = 15.5
    keyX = 0.975
    set key vertical Left reverse width -0.5 height +0 font ",16"
    set key opaque
    set key autotitle columnheader
    set key bottom right spacing -1
    
    #Format of axis numbers
    set format xy '$\%g$'
    set format x '\footnotesize \%10.1f'
    set format y '\footnotesize \%10.1f'
    #Format tics
    set xtics 0,0.1 out nomirror
    set xtics offset -0.2,0
    set mxtics 5
    set ytics 0.6,0.1 out nomirror
    set ytics offset 0.4,0
    set mytics 5
    #Background grid setting
    set grid
    show grid
    set object 1 rectangle from 0.132, graph 0 to 0.456, graph 1 fillcolor rgb "#A9A9A9" fs pattern 1 noborder behind
    #Margins
    set lmargin 5.9
    set rmargin 0.5
    set bmargin 3.5
    #Axis range settings
    set xrange [0:0.535]
    set yrange [0.6:1.2]
    #Format lines, boxes...
    set style line 4 lt 1 lc rgb 'black' lw 2 pt 13 ps 1.25 dt 4
    set style line 5 lt 1 lc rgb 'black' lw 2 pt 4 ps 1.0 dt 5
    #Multiplot
    set multiplot
    set origin 0,0
    set size 1,1
    #Plots
    set key at graph keyX, character keyY
    
    #simulate key box
    set object 2 rectangle from graph keyX, character keyY + 0.5*keySpacing to graph 0.65, character keyY - 1.5*keySpacing fillcolor rgb "#FFFFFF" fs pattern 2 border rgb "black"
    
    set xlabel '\small $r/D_T\;[-]$' offset 0,+0
    set ylabel '\small $c_{m2} \cdot A_{T}/Q_T\;[-]$' offset +10.5,+0
    
    plot \
      'dataset.dat' using 1:2 with lines ls 4, \
      'dataset.dat' using 1:2 with points ls 4 lc rgb "#71da71" title " "
    
    #unset these so that they are not duplicated by the following plot command
    unset border
    unset xtics
    unset ytics
    unset xlabel
    unset ylabel
    
    unset object 1
    unset object 2
    
    #Last Plot
    keyY = keyY - keySpacing
    set key at graph keyX, character keyY
    
    plot \
      'dataset.dat' using 1:3 with lines ls 5, \
      'dataset.dat' using 1:3 with points ls 5 lc rgb "#4da6ff" title " "
    

    This then produces (I used standalone epslatex terminal. It might be some font issue, but it seems that the ylabel would benefit from slightly larger horizontal offset):

    enter image description here