Ask an expert. Trust the answer.

Your academic and career questions answered by verified experts

how to remove the gap between subplots and around [duplicate]

Date: 2022-07-28 11:04:28

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 

Answer: 

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);

 


Why Matlabhelpers ?

Our Matlab assignment helpers for online MATLAB assignment help service take utmost care of your assignments by keeping the codes simple yet of high-quality. We offer the most reliable MATLAB solutions to students pursuing their Computer Science course from the Monash University, the University of Sydney, the University of New South Wales, the University of Melbourne; to name a few. Approach us today for best Matlab solutions online!

whatsApp order on matlabhelpers.com

telegram order on matlabsolutions.com