Search code examples
gnuplot

Overriding Gnuplot Multiplot Layout


According to the docs, a multiplot with a layout can have a plot with its own custom origin which overrides its layout location: http://gnuplot.sourceforge.net/docs_4.2/node203.html

I am trying to do this with the following example code; however, I cannot get the third plot to align in the middle of the second row.

set multiplot layout 2,2 rowsfirst margins 0.1,0.93,0.2,0.93 spacing 0.1,0.1
plot sin(x)
plot cos(x)
plot tan(x)

How do I make tan(x) appear in the middle of the second row, instead of in the first cell of the second row?

Here is what I get:

Here is what I want: enter image description here

I do understand that I can simply turn off the layout and manually set the size / origin of each plot to get what I want; however, I am looking for a solution that allows me to work the layout specification, as I am working with a margin as well that I'd rather not define with different code unless it is absolutely necessary to get the effect I am looking for.


Solution

  • One must admit that it is probably a bit tedious, on the other hand, directly playing with margins gives you certain flexibility. The script below basically just first calculates the width(s) and height(s) in screen coordinates of individual plots and then positions them separately via the set margin command:

    BORDER_L = 0.10
    BORDER_R = 0.07
    BORDER_B = 0.20
    BORDER_T = 0.07
    
    SPACING_X = 0.10
    SPACING_Y = 0.10
    
    NUM_ROWS = 2
    NUM_COLS = 2
    
    PLT_W = (1 - BORDER_L - BORDER_R - (NUM_COLS-1)*SPACING_X)/NUM_COLS
    PLT_H = (1 - BORDER_B - BORDER_T - (NUM_ROWS-1)*SPACING_Y)/NUM_ROWS
    
    #set multiplot layout 2,2 rowsfirst margins BORDER_L,1-BORDER_R,BORDER_B,1-BORDER_T spacing SPACING_X,SPACING_Y
    
    set multiplot
    
    set tmargin at screen 1 - BORDER_T
    set bmargin at screen 1 - BORDER_T - PLT_H
    set lmargin at screen BORDER_L
    set rmargin at screen BORDER_L + PLT_W
    
    plot sin(x)
    
    set tmargin at screen 1 - BORDER_T
    set bmargin at screen 1 - BORDER_T - PLT_H
    set lmargin at screen 1 - BORDER_R - PLT_W
    set rmargin at screen 1 - BORDER_R
    
    plot cos(x)
    
    set tmargin at screen 1 - BORDER_T - PLT_H - SPACING_Y
    set bmargin at screen BORDER_B 
    set lmargin at screen (1 - PLT_W)/2
    set rmargin at screen 1 - (1 - PLT_W)/2
    
    plot tan(x)
    

    This then produces: enter image description here