Search code examples
gnuplot

GNUplot stacked multiplot - Top plot does not align perfectly


I'm trying to make a stacked plot with GNUplot.

TMARGIN = 0.05
BMARGIN = 0.15
LMARGIN = 0.15
RMARGIN = 0.0

TCOORD=1-TMARGIN
BCOORD=1-BMARGIN

DY = 0.2

# Ratio
set size ratio 0.5

# Tics
unset xtics
unset ytics

# Multiplot
set multiplot layout 4, 1
unset key
unset title

# Global
set lmargin at screen LMARGIN
set rmargin at screen RMARGIN

# Plot 0
set tmargin at screen TCOORD; set bmargin at screen TCOORD-DY;
plot sin(x)

# Plot 1
set tmargin at screen TCOORD-DY; set bmargin at screen TCOORD-(2*DY)
plot sin(x)

# Plot 2
set tmargin at screen TCOORD-(2*DY); set bmargin at screen TCOORD-(3*DY)
plot sin(x)

# Plot 3
set tmargin at screen TCOORD-(3*DY); set bmargin at screen TCOORD-(4*DY)
set xlabel "xlabel"
plot sin(x)

With the script above I end up with the top plot not aligned to the others.

enter image description here

Out of ideas. Grateful for any help.

Thanks in advance!

Edit: This is "Version 5.4 patchlevel 2" on Archlinux if relevant.


Solution

  • Your script tries to use three different methods to adjust the plot positions: set ratio, set multiplot layout, and set margin. They are fighting each other. If I understand the intent correctly, this can all be done in the multiplot layout command. If the 2:1 ratio is important, you may have to calculate the right margin manually and/or adjust the width of the output using the size parameter of set term.

    # No extra stuff around the edges
    unset xtics
    unset ytics
    unset key
    unset title
    
    # Multiplot
    set multiplot layout 4, 1 margins screen 0.15, 1.0, 0.1, 1.0 spacing 0,0
    
    # Plot 0
    plot sin(x)
    
    # Plot 1
    plot sin(x)
    
    # Plot 2
    plot sin(x)
    
    # Plot 3
    set xlabel "xlabel"
    plot sin(x)
    
    unset multi
    

    enter image description here