How to reduce space between plots in subplot and how to increase the area of plot in subplot
Show older comments

I have the created the following vector plots by using subplot but, there are larger wider space in between plots in horizontal direction and the area of plots is also less. Kindly help me with the problem. please fid below the code for the plotted vetor field
clear all;close all;clc
format long
%--------Reading velocity data-------%
fid=fopen('50112.txt');
fulltext=textscan(fid,'%s','delimiter','\n');
numberofgridpoints=length(fulltext{1});
fclose(fid);
n=numberofgridpoints/200;
fid=fopen('50112.txt');
datamatrix=fscanf(fid,'%f%f%f%f',[4,inf]);
%-------stroing of velocity data -----------%
for i=1:200
c=i-1;
for j=1:n
d=(c*n)+j;
x(j)=datamatrix(1,j);
y(j)=datamatrix(2,j);
u(i,j)=datamatrix(3,d);
v(i,j)=datamatrix(4,d);
end
end
%*********************************************%
%-------Proper Orthogonal Decompostion--------%
%*********************************************%
%---Calcalating the covarience matrix---------%
c1=u*u';
c2=v*v';
c=(c1+c2)/200;
[beta, lmd]=svd(c);
phix=u'*beta;
phiy=v'*beta;
for j=1:200
phinor=0;
for i=1:n
phinor=phinor+phix(i,j)^2+phiy(i,j)^2;
end
phinor=sqrt(phinor);
phix(:,j)=phix(:,j)/phinor;
phiy(:,j)=phiy(:,j)/phinor;
end
timecoeu=u*phix;
timecoev=v*phiy;
timecoe=timecoeu+timecoev;
%---------velocity recosntruction------------%
for i=1:n
for j=200
meanu(i,j)=0;
coherentu(i,j)=0;
transitionu(i,j)=0;
turbulentu(i,j)=0;
meanv(i,j)=0;
coherentv(i,j)=0;
transitionv(i,j)=0;
turbulentv(i,j)=0;
tvmean(i,j)=0;
tvcoherentv(i,j)=0;
tvtransition(i,j)=0;
tvturbulent(i,j)=0;
end
end
for i=1:200
for j=1:1
meanu(:,i)=timecoe(i,j)*phix(:,j)+meanu(:,i);
meanv(:,i)=timecoe(i,j)*phiy(:,j)+meanv(:,i);
end
end
for i=1:200
for j=2:9
coherentu(:,i)=timecoe(i,j)*phix(:,j)+coherentu(:,i);
coherentv(:,i)=timecoe(i,j)*phiy(:,j)+coherentv(:,i);
end
end
for i=1:200
for j=10:186
transitionu(:,i)=timecoe(i,j)*phix(:,j)+ transitionu(:,i);
transitionv(:,i)=timecoe(i,j)*phiy(:,j)+ transitionv(:,i);
end
end
for i=1:200
for j=187:200
turbulentu(:,i)=timecoe(i,j)*phix(:,j)+ turbulentu(:,i);
turbulentv(:,i)=timecoe(i,j)*phiy(:,j)+ turbulentv(:,i);
end
end
%-----------CYCLE 1------------%
subplot(4,2,1)
tvmean(:,1)=sqrt(meanu(:,1).^2+meanv(:,1).^2);
tvmeanpart=reshape(tvmean(:,1),[13,20]);
[X,Y]=meshgrid(397:32:1005,157:32:541);
contourf(X,Y,tvmeanpart,100,'linestyle','none')
hold on
quiver(x,y,meanu(:,1).',meanv(:,1).','color','black');
set(gca,'ydir','reverse');
xlabel('x (mm)','fontweight','normal','fontsize',8)
ylabel('y (mm)','fontweight','normal','fontsize',8)
title('mean part (#1)','fontweight','bold','fontsize',9)
axis on
axis tight
hold off
c = colorbar;
c.Label.String = 'V [m/s]'
c.Label.FontSize =8;
c.FontWeight = 'normal'
subplot(4,2,3)
tvcoherent(:,1)=sqrt(coherentu(:,1).^2+coherentv(:,1).^2);
tvcoherentpart=reshape(tvcoherent(:,1),[13,20]);
[X,Y]=meshgrid(397:32:1005,157:32:541);
contourf(X,Y,tvcoherentpart,100,'linestyle','none')
hold on
quiver(x,y,coherentu(:,1).',coherentv(:,1).','color','black');
set(gca,'ydir','reverse');
xlabel('x (mm)','fontweight','normal','fontsize',8)
ylabel('y (mm)','fontweight','normal','fontsize',8)
title('coherent part (#1)','fontweight','bold','fontsize',9)
axis on
axis tight
hold off
c = colorbar;
c.Label.String = 'V [m/s]'
c.Label.FontSize =8;
c.FontWeight = 'normal'
subplot(4,2,5)
tvtransition(:,5)=sqrt(transitionu(:,5).^2+transitionv(:,5).^2);
tvtransitionpart=reshape(tvtransition(:,5),[13,20]);
[X,Y]=meshgrid(397:32:1005,157:32:541);
contourf(X,Y,tvtransitionpart,100,'linestyle','none')
hold on
quiver(x,y,transitionu(:,5).',transitionv(:,5).','color','black');
set(gca,'ydir','reverse');
xlabel('x (mm)','fontweight','normal','fontsize',8)
ylabel('y (mm)','fontweight','normal','fontsize',8)
title('transition part (#1)','fontweight','bold','fontsize',9)
axis on
axis tight
hold off
c = colorbar;
c.Label.String = 'V [m/s]'
c.Label.FontSize =8;
c.FontWeight = 'normal'
subplot(4,2,7)
tvturbulent=sqrt(turbulentu(:,1).^2+turbulentv(:,1).^2);
tvturbulentpart=reshape(tvturbulent,[13,20]);
[X,Y]=meshgrid(397:32:1005,157:32:541);
contourf(X,Y,tvturbulentpart,100,'linestyle','none')
hold on
quiver(x,y,turbulentu(:,1).',turbulentv(:,1).','color','black');
set(gca,'ydir','reverse');
xlabel('x (mm)','fontweight','normal','fontsize',8)
ylabel('y (mm)','fontweight','normal','fontsize',8)
title('turbulent part (#1)','fontweight','bold','fontsize',9)
axis on
axis tight
hold off
c = colorbar;
c.Label.String = 'V [m/s]'
c.Label.FontSize =8;
c.FontWeight = 'normal'
%----------END of CYCLE 1-------%
%-----------CYCLE 5------------%
subplot(4,2,2)
tvmean(:,5)=sqrt(meanu(:,5).^2+meanv(:,5).^2);
tvmeanpart=reshape(tvmean(:,5),[13,20]);
[X,Y]=meshgrid(397:32:1005,157:32:541);
contourf(X,Y,tvmeanpart,100,'linestyle','none')
hold on
quiver(x,y,meanu(:,5).',meanv(:,5).','color','black');
set(gca,'ydir','reverse');
xlabel('x (mm)','fontweight','normal','fontsize',8)
ylabel('y (mm)','fontweight','normal','fontsize',8)
title('mean part (#5)','fontweight','bold','fontsize',9)
axis on
axis tight
hold off
c = colorbar;
c.Label.String = 'V [m/s]'
c.Label.FontSize =8;
c.FontWeight = 'normal'
subplot(4,2,4)
tvcoherent(:,5)=sqrt(coherentu(:,5).^2+coherentv(:,5).^2);
tvcoherentpart=reshape(tvcoherent(:,5),[13,20]);
[X,Y]=meshgrid(397:32:1005,157:32:541);
contourf(X,Y,tvcoherentpart,100,'linestyle','none')
hold on
quiver(x,y,coherentu(:,5).',coherentv(:,5).','color','black');
set(gca,'ydir','reverse');
xlabel('x (mm)','fontweight','normal','fontsize',8)
ylabel('y (mm)','fontweight','normal','fontsize',8)
title('coherent part (#5)','fontweight','bold','fontsize',9)
axis on
axis tight
hold off
c = colorbar;
c.Label.String = 'V [m/s]'
c.Label.FontSize =8;
c.FontWeight = 'normal'
subplot(4,2,6)
tvtransition(:,5)=sqrt(transitionu(:,5).^2+transitionv(:,5).^2);
tvtransitionpart=reshape(tvtransition(:,5),[13,20]);
[X,Y]=meshgrid(397:32:1005,157:32:541);
contourf(X,Y,tvtransitionpart,100,'linestyle','none')
hold on
quiver(x,y,transitionu(:,5).',transitionv(:,5).','color','black');
set(gca,'ydir','reverse');
xlabel('x (mm)','fontweight','normal','fontsize',8)
ylabel('y (mm)','fontweight','normal','fontsize',8)
title('transition part (#5)','fontweight','bold','fontsize',9)
axis on
axis tight
hold off
c = colorbar;
c.Label.String = 'V [m/s]'
c.Label.FontSize =8;
c.FontWeight = 'normal'
subplot(4,2,8)
tvturbulent=sqrt(turbulentu(:,5).^2+turbulentv(:,5).^2);
tvturbulentpart=reshape(tvturbulent,[13,20]);
[X,Y]=meshgrid(397:32:1005,157:32:541);
contourf(X,Y,tvturbulentpart,100,'linestyle','none')
hold on
quiver(x,y,turbulentu(:,5).',turbulentv(:,5).','color','black');
set(gca,'ydir','reverse');
xlabel('x (mm)','fontweight','normal','fontsize',8)
ylabel('y (mm)','fontweight','normal','fontsize',8)
title('turbulent part (#5)','fontweight','bold','fontsize',9)
axis on
axis tight
hold off
c = colorbar;
c.Label.String = 'V [m/s]'
c.Label.FontSize =8;
c.FontWeight = 'normal'
%----------END of CYCLE 5-------%
9 Comments
DGM
on 23 Apr 2021
That looks like an awfully wide spacing to be normal. Without knowing how you arranged the plots, I can't do anything but guess why it's like that.
HARIKRISHNA B YADULADODDI
on 23 Apr 2021
the cyclist
on 23 Apr 2021
Can you also upload the variables used, so that we can reproduce your results, and experiment?
DGM
on 23 Apr 2021
The code you posted is for a [3 2] subplot arrangement, but the image is [4 2]. Either way, I suspect that the only thing going on here is your selection of window/figure aspect ratio. The aspect ratio of the individual axes are constrained by the properties you've set (i.e. axis equal), so they can't scale to fit the overall window geometry. I'd either try to adjust the window geometry or find a better way to arrange the subplots. Whenever I have a scenario where I want to plot a lot of subplots in a "tall" figure, I split the task up into pages and concatenate them afterward. There are probably more elegant solutions.
HARIKRISHNA B YADULADODDI
on 24 Apr 2021
DGM
on 24 Apr 2021
You're not wrong to use axis equal. If you want the data axes to remain proportional, use it. The problem is simply the relationship between the aspect ratio of the plots (constrained by 'axis equal') and the aspect ratio of the figure (unconstrained, but limited ultimately by the screen geometry).
Consider the example. Here I have a bunch of plots with fixed aspect ratio. Changing the figure geometry affects the spacing.



I just manually resized the window to do that. You could also do it by setting the figure's position property, if you wanted a programmatic way of doing it.
Adam Danz
on 24 Apr 2021
You're already using axis tight which reduces the axes to the range of your data. What additional reduction do you expect to have? If your axes are adjusted to the range of the contour plot, some of the quiver arrows will be cut out. If you want the quiver arrows to extend beyond the edge of the axes set their clipping property to off and then set the axis limits based on the ranges of data in the contour plot.
HARIKRISHNA B YADULADODDI
on 26 Apr 2021
DGM
on 26 Apr 2021
I resized the figure window by clicking on the corner and dragging it.
Answers (2)
the cyclist
on 23 Apr 2021
Edited: Adam Danz
on 26 Apr 2021
1 vote
This might require an overhaul of your code, but I would say you can probably achieve better results with the new(er) tiledlayout method of creating subplots.
6 Comments
HARIKRISHNA B YADULADODDI
on 26 Apr 2021

Tiledlayout might be a lot more convenient, but it can't solve the basic underlying problem here. The aspect ratio of the objects in the layout is fixed. If the figure geometry is fixed, they can ultimately only fill a certain fraction of the width.
Consider the approximate figure geometry of 600x850. The tight geometry of each axes+labels+cb+title group is about 135x235. If these objects are scaled up to fit the vertical extents of the figure, they will only cover about 600x520, i.e. only about 60% of the figure width. If more coverage is desired, one of these constraints has to change:
- Either axes geometry must change (not really a good option here)
- Tiling layout must change (not really a good option here either)
- Figure geometry must change (I don't see a reason why this can't be changed)
I posted an example before using subplot and default spacing. I redid it using tiledlayout with no spacing and no padding, with figure and axes aspect ratios (roughly) approximate to those in the OP's figure. The figure is simply wider than the content can fill. I don't see the problem with just resizing the window to fit the content. Based on the above rough calculations, a figure aspect ratio of 1.1 to 1.2 should fit the content better, but just grabbing the window corner and moving it hardly requires that much effort or precision.

@DGM The problem posed by the OP is to reduce the space between axes and there are lots of ways to solve that -- all of which include resizing the figure due to the uniform data aspect ratio as you mentioned.
The solution with the most flexibility is to manually create axes with non-normalized coordinates but that's a lot of work for a user who appears to be toward the beginners end of the spectrum. Subplot produces normalized coordinates which can later be changed but I'd argue that it's more difficult than taking advantage of the spacing properties of tiledlayout.
Below is a direct comparison of the same data and same figure size using suplot and tiledlayout. The latter clearly has a greater reduction in inter-axes space than with axes created by subplot.
f1 = figure();
f2 = figure();
tlo = tiledlayout(f1,4,2,'Padding','compact','TileSpacing','compact');
nrow = 4;
ncol = 2;
x = linspace(400,1000,500);
y = linspace(250,550,500);
xr = linspace(-2*pi,2*pi,numel(x));
yr = linspace(0,4*pi,numel(y));
[xm,ym] = meshgrid(xr,yr);
Z = sin(xm) + cos(ym);
for i = 1:8
ax = nexttile(tlo);
contourf(ax,x,y,Z,10)
axis(ax,'tight')
axis(ax,'equal')
colorbar(ax)
ax2 = subplot(nrow,ncol,i,'Parent',f2);
contourf(ax2,x,y,Z,10)
axis(ax2,'tight')
axis(ax2,'equal')
colorbar(ax2)
end
f1.Position(3:4) = [540,500];
movegui(f1)
title(tlo,'TiledLayout')
annotation(f1,'rectangle',[0 0 1 1], 'Color','k','LineWidth',4) %show fig edges
f2.Position(3:4) = [540,500];
movegui(f2)
sgtitle(f2,'subplot')
annotation(f2,'rectangle',[0 0 1 1], 'Color','k','LineWidth',4) %show fig edges
@HARIKRISHNA B YADULADODDI if you really want to optimize space, it looks like axes within each row use the same color scale so you could eliminate the colorbars on the left.
Also, it looks like all axes have the same limits so you could remove axis ticks for all axes except the ones on the left and bottom.
% quick demo
f1 = figure();
tlo = tiledlayout(f1,4,2,'Padding','compact','TileSpacing','none');
x = linspace(400,1000,500);
y = linspace(250,550,500);
xr = linspace(-2*pi,2*pi,numel(x));
yr = linspace(0,4*pi,numel(y));
[xm,ym] = meshgrid(xr,yr);
Z = sin(xm) + cos(ym);
ax = gobjects(1,8);
for i = 1:8
ax(i) = nexttile(tlo);
contourf(ax(i),x,y,Z,10)
axis(ax(i),'tight')
axis(ax(i),'equal')
if mod(i,2)==0
colorbar(ax(i))
end
end
f1.UserData.lp(1) = linkprop(ax(1:6),'clim');
f1.UserData.lp(2) = linkprop(ax(7:8),'clim');
f1.UserData.lp(3) = linkprop(ax,{'xlim','ylim'});
set(ax(2:2:end-2),'xtick',[],'ytick',[])
set(ax(end),'ytick',[])
set(ax(1:2:end-3),'xtick',[])
set(ax(end-1:end),'XTick',ax(end).XTick(1:end-1)) % only needed of last tick is in the way
f1.Position(3:4) = [540,500];
movegui(f1)
title(tlo,'TiledLayout')
annotation(f1,'rectangle',[0 0 1 1], 'Color','k','LineWidth',4) %show fig edges
HARIKRISHNA B YADULADODDI
on 27 Apr 2021
Matt J
on 26 Apr 2021
0 votes
You can also replace subplot with subaxis
6 Comments
HARIKRISHNA B YADULADODDI
on 27 Apr 2021
Matt J
on 27 Apr 2021
If I simply do a global search/replace in your code, changing "subplot" to "subaxis", I get the following,

LIU
on 21 Mar 2022
Thank you very much, this helps me a lot indeed.
DGM
on 21 Mar 2022
LIU isn't OP.
Categories
Find more on Labels and Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


