How to remove horizontal spacing between subplots?

조회 수: 262 (최근 30일)
A LL
A LL 2022년 4월 6일
댓글: Cris LaPierre 2022년 7월 26일
I have a figure with 2x2 subplots. I want to make the subplots side by side so that there is no spacing between the subplots.
I used the "subplottight" function written by Brian D'Alessandro here: https://www.briandalessandro.com/blog/how-to-make-a-borderless-subplot-of-images-in-matlab/
%subplottight function by Brian D'Alessandro:
function h = subplottight(n,m,i)
[c,r] = ind2sub([m n], i);
ax = subplot('Position', [(c-1)/m, 1-(r)/n, 1/m, 1/n])
if(nargout > 0)
h = ax;
end
end
I works like a charm to remove the vertical spacing between my subplots but I am still left with spacing vertically (see figure below).
How can I modify the "subplotitght" function to also remove the space horizontally?
Thank you

채택된 답변

Cris LaPierre
Cris LaPierre 2022년 4월 6일
Consider using tiledlayout instead of subplot. You can then use the TileSpacing input to adjust the spacing.
t = tiledlayout(2,2,'TileSpacing','none');
  댓글 수: 9
Maximilian Grobbelaar
Maximilian Grobbelaar 2022년 7월 26일
I'm having a similar problem where after i change my DataAspectRatio for my two subplots, it creates a huge space in between them, even though my TileSpacing = 'none'.
Cris LaPierre
Cris LaPierre 2022년 7월 26일
This is likely caused by a similar issue.
Stretch-to-Fill
When the “stretch-to-fill” behavior is enabled, MATLAB stretches the axes to fill the available space. The axes might not exactly match the data aspect ratio, plot box aspect ratio, and camera-view angle values stored in its DataAspectRatio, PlotBoxAspectRatio, and CameraViewAngle properties.
If you specify the data aspect ratio, plot box aspect ratio, or camera-view angle, then the “stretch-to-fill” behavior is disabled. When the behavior is disabled, MATLAB makes the axes as large as possible within the available space and strictly adheres to the property values. There is no distortion. For more information, see Control Axes Layout.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Voss
Voss 2022년 4월 6일
It looks like plotting the alphaShapes (e.g., LITS31) sets the DataAspectRatioMode of the axes to 'manual' so that although the Positions of the axes span the space allotted by the tiledlayout with TileSpacing 'none', the actual space used is less than that in order to maintain the 1:1 X:Y aspect ratio.
One way to get around that is to set each axes' DataAspectRatioMode back to 'auto' after the alphaShape is plotted. And, since now the text objects (e.g., W31-32) can overlap with other axes, it is also necessary to change the child order of the axes at the end.
I'm not sure that allowing an aspect ratio that's not 1:1 is the best thing to do though, since it means your maps can be distorted (i.e., different scales in Lat and Long direction).
load('data.mat');
%% Figure
figure
t = tiledlayout(2,2,'TileSpacing','none');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%W31-32
% Tile 1
nexttile
%subplottight(2,2,1)
%plot the land (grey) and and oceans (white)
p=pcolor(Land);
p.Annotation.LegendInformation.IconDisplayStyle = 'off';
colormap(cmap);
hold on;
shading flat;
axis ij;
% axis equal;
box on;
axis off;
get(gca(),'DataAspectRatioMode')
ans = 'auto'
get(gca(),'DataAspectRatio')
ans = 1×3
180 180 1
%plot alphaShape for LITS
h = plot(LITS31,'FaceColor',cbrewer3(5,:),'EdgeColor','none');
get(gca(),'DataAspectRatioMode')
ans = 'manual'
get(gca(),'DataAspectRatio')
ans = 1×3
1 1 1
set(gca(),'DataAspectRatioMode','auto');
%contour for starting position
[M1,c1] = contour(xx,yy,EASE31,1,'LineWidth',4.5,'Color', cbrewer3(9,:));
%contour for ending position
[M2,c2] = contour(xx,yy,EASE32,1,'LineWidth',4,'Color',cbrewer3(11,:));
mask = contour(xx,yy,Mask,1,'LineWidth',1,'Color','black');
h4 = plot(Xstorm,Ystorm, '--o','LineWidth',2,'MarkerSize',7,'Color','red','MarkerFaceColor','red','MarkerEdgeColor','red');
text(Xstorm(1),Ystorm(1),' 02 Aug','FontSize',14);
text(Xstorm(end),Ystorm(end),' 14 Aug','FontSize',14);
xlim([110 270]);
ylim([70 260]);
text(225,79,'W31-32','Fontsize',18)
get(gca(),'DataAspectRatioMode')
ans = 'auto'
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%W32-33
% Tile 2
nexttile
%subplottight(2,2,2)
%plot the land (grey) and and oceans (white)
p=pcolor(Land);
p.Annotation.LegendInformation.IconDisplayStyle = 'off';
colormap(cmap);
hold on;
shading flat;
axis ij;
% axis equal;
box on;
axis off;
%plot alphaShape for LITS
h = plot(LITS32,'FaceColor',cbrewer3(5,:),'EdgeColor','none');
%plot akphaShape for ridging and FS export
h3 = plot(OUT32,'FaceColor',cbrewer3(3,:),'EdgeColor','none');
%plot alphaShape for export
h2 = plot(EXPORT32,'FaceColor',cbrewer3(1,:),'EdgeColor','none');
set(gca(),'DataAspectRatioMode','auto');
%contour for starting position
[M1,c1] = contour(xx,yy,EASE32,1,'LineWidth',4.5,'Color', cbrewer3(9,:));
%contour for ending position
[M2,c2] = contour(xx,yy,EASE33,1,'LineWidth',4,'Color',cbrewer3(11,:));
mask = contour(xx,yy,Mask,1,'LineWidth',1,'Color','black');
h4 = plot(Xstorm,Ystorm, '--o','LineWidth',2,'MarkerSize',7,'Color','red','MarkerFaceColor','red','MarkerEdgeColor','red');
text(Xstorm(1),Ystorm(1),' 02 Aug','FontSize',14);
text(Xstorm(end),Ystorm(end),' 14 Aug','FontSize',14);
xlim([110 270]);
ylim([70 260]);
text(225,79,'W32-33','Fontsize',18)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%W33-34
% Tile 3
nexttile
%subplottight(2,2,3)
%plot the land (grey) and and oceans (white)
p=pcolor(Land);
p.Annotation.LegendInformation.IconDisplayStyle = 'off';
colormap(cmap);
hold on;
shading flat;
axis ij;
% axis equal;
box on;
axis off;
%plot alphaShape for LITS
h = plot(LITS33,'FaceColor',cbrewer3(5,:),'EdgeColor','none');
set(gca(),'DataAspectRatioMode','auto');
%contour for starting position
[M1,c1] = contour(xx,yy,EASE33,1,'LineWidth',4.5,'Color', cbrewer3(9,:));
%contour for ending position
[M2,c2] = contour(xx,yy,EASE34,1,'LineWidth',4,'Color',cbrewer3(11,:));
mask = contour(xx,yy,Mask,1,'LineWidth',1,'Color','black');
h4 = plot(Xstorm,Ystorm, '--o','LineWidth',2,'MarkerSize',7,'Color','red','MarkerFaceColor','red','MarkerEdgeColor','red');
text(Xstorm(1),Ystorm(1),' 02 Aug','FontSize',14);
text(Xstorm(end),Ystorm(end),' 14 Aug','FontSize',14);
xlim([110 270]);
ylim([70 260]);
text(225,79,'W33-34','Fontsize',18)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%W34-35
% Tile 4
nexttile
%subplottight(2,2,4)
%plot the land (grey) and and oceans (white)
p=pcolor(Land);
p.Annotation.LegendInformation.IconDisplayStyle = 'off';
colormap(cmap);
hold on;
shading flat;
axis ij;
% axis equal;
box on;
axis off;
%plot alphaShape for LITS
h = plot(LITS34,'FaceColor',cbrewer3(5,:),'EdgeColor','none');
%plot alphaShape for export
h2 = plot(EXPORT34,'FaceColor',cbrewer3(1,:),'EdgeColor','none');
set(gca(),'DataAspectRatioMode','auto');
%contour for starting position
[M1,c1] = contour(xx,yy,EASE34,1,'LineWidth',4.5,'Color', cbrewer3(9,:));
%contour for ending position
[M2,c2] = contour(xx,yy,EASE35,1,'LineWidth',4,'Color',cbrewer3(11,:));
mask = contour(xx,yy,Mask,1,'LineWidth',1,'Color','black');
h4 = plot(Xstorm,Ystorm, '--o','LineWidth',2,'MarkerSize',7,'Color','red','MarkerFaceColor','red','MarkerEdgeColor','red');
text(Xstorm(1),Ystorm(1),' 02 Aug','FontSize',14);
text(Xstorm(end),Ystorm(end),' 14 Aug','FontSize',14);
h4.Annotation.LegendInformation.IconDisplayStyle = 'off';
xlim([110 270]);
ylim([70 260]);
text(225,79,'W34-35','Fontsize',18)
% reorder the axes so no texts are obscured:
set(t,'Children',flipud(get(t,'Children')))
  댓글 수: 1
A LL
A LL 2022년 4월 6일
Ok, interesting. Thank you for your time.
However, the figure does seem to be distorsted compare to my initial figure.
I guess I will just keep the "adjust figure window" solution.
I end up with this:

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by