Search code examples
matlabmarginssubplot

how to remove the gap between subplots and around


I am plotting two subplots (2x1) in one figure. I would like to remove all the spacing between two subplots and remove the xlable and xlabel ticks for the top subplot too. Also, I am trying to remove all the spacing outside the subplot. I try

set(gca, 'LooseInset', get(gca,'TightInset'))

But it doesn't work. Now I am removing those margins and labels manually, I have 60 figures need to be handled and doing all those manually is time consuming. Any better way to do it? Thanks.

I also try the subtightplot, it helps to reduce all the margins but the xlabel and ylabel are also cut

margins=[0 0];
t = 0:0.01:10;
y1 = sin(t);
y2 = cos(t);
h1 = subtightplot(2,1,1, margins);
plot(t, y1);
ystr = {'sin(x)','(dimensionless)'}
hy1 = ylabel(ystr);
set(gca, 'fontsize', 14);
set(hy1, 'fontsize', 14);
set(gca, 'XTickLabel', [],'XTick',[])

h2 = subtightplot(2,1,2,margins);
plot(t, y2, 'r-o');
hx2=xlabel('frequency');
hy2=ylabel('amplitude');
set(gca, 'fontsize', 14);
set(hx2, 'fontsize', 14);
set(hy2, 'fontsize', 14);

I also try the subplot_tight but it is even worse


Solution

  • you can use subplot_tight or subtightplot from the FEX. to remove all x-tick and labels use:

    set(gca, 'XTickLabel', [],'XTick',[])
    

    in the appropriate subplot...

    Edit:

    Since you do want to include the labels etc, you can so it using the position handle in axes:

    t = 0:0.01:10;
    y1 = sin(t);
    y2 = cos(t);
    
    
    left= 0.15;
    bottom1=0.5;
    bottom2=0.05;
    width=0.8;
    height=0.45; % which is also bottom1-bottom2
    
    axes('Position',[left bottom1 width height]);
    plot(t, y1);
    ystr = {'sin(x)','(dimensionless)'}
    hy1 = ylabel(ystr);
    set(gca, 'fontsize', 14);
    set(hy1, 'fontsize', 14);
    set(gca, 'XTickLabel', [],'XTick',[])
    
    
    axes('Position',[left bottom2 width height])
    plot(t, y2, 'r-o');
    hx2=xlabel('frequency');
    hy2=ylabel('amplitude');
    set(gca, 'fontsize', 14);
    set(hx2, 'fontsize', 14);
    set(hy2, 'fontsize', 14);
    

    enter image description here