How to remove horizontal spacing between subplots?
260 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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
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
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
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')
get(gca(),'DataAspectRatio')
%plot alphaShape for LITS
h = plot(LITS31,'FaceColor',cbrewer3(5,:),'EdgeColor','none');
get(gca(),'DataAspectRatioMode')
get(gca(),'DataAspectRatio')
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')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%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')))
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!