How to remove horizontal spacing between subplots?

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

 Respuesta aceptada

Consider using tiledlayout instead of subplot. You can then use the TileSpacing input to adjust the spacing.
t = tiledlayout(2,2,'TileSpacing','none');

9 comentarios

A LL
A LL el 6 de Abr. de 2022
Editada: A LL el 6 de Abr. de 2022
I tried the following:
figure
t = tiledlayout(2,2,'TileSpacing','none');
% Tile 1
nexttile
%...
% Tile 2
nexttile
%...
% Tile 3
nexttile
%...
% Tile 4
nexttile
%...
But I still have the same issue as before. It removes the vertical spacing but I am left with the horizontal spacing.
Thank you
Perhaps there is white space in your plot? How do you create each one?
As you can see from the documentation, 'none' removes all spacing between the axes.
A LL
A LL el 6 de Abr. de 2022
Editada: A LL el 6 de Abr. de 2022
I am not aware of creating any white space in my plot. What command would generate a white space between plots?
I have attached my data in "data.mat"
Here is my code for the figure:
%% 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;
%plot alphaShape for LITS
h = plot(LITS31,'FaceColor',cbrewer3(5,:),'EdgeColor','none');
%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)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%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');
%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');
%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');
%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)
I still obtain the undesired horizontal spacing
Thank you
Cris LaPierre
Cris LaPierre el 6 de Abr. de 2022
Editada: Cris LaPierre el 6 de Abr. de 2022
Can you share your data? Save your variables to a mat file and attach it to your post using the paperclip icon.
Another simple test to run is creating a figure of just one of your plots with the axis on to see what is happening.
%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;
%plot alphaShape for LITS
h = plot(LITS31,'FaceColor',cbrewer3(5,:),'EdgeColor','none');
%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)
A LL
A LL el 6 de Abr. de 2022
Editada: A LL el 6 de Abr. de 2022
I have attached my data in my previous comment with the code to generate the figure. Thank you.
Here is what I obtain by running the "simple test" above:
Not sure I have a great answer, but I was at least able to isolate the cause. What you are seeing is a result of using axis equal. If you consult the axis documentation page, the last line describing equal says "This style disables the default “stretch-to-fill” behavior."
It also appears that plotting an alphashape automatically sets the axis style to equal. These include your EXPORT, LITS, and OUT32 variables.
With axis set to equal, the axis aspect ratio is preserved, which means that all the available space may not be filled. One way to fix this is to manually adjust the size of your figure window. You could also change your layout to be 1x4 instead of 2x2.
A LL
A LL el 6 de Abr. de 2022
Oh ok, interesting. Thank you very much for your time.
I ended reducing the size of my figure window and saved it as a jpeg so it does look like there is no space.
I was hoping for a more elegant way, but it will do! Thanks again :)
It looks like this:
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'.
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.

Iniciar sesión para comentar.

Más respuestas (1)

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 comentario

A LL
A LL el 6 de Abr. de 2022
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:

Iniciar sesión para comentar.

Categorías

Más información sobre Contour Plots en Centro de ayuda y File Exchange.

Productos

Versión

R2021a

Preguntada:

el 6 de Abr. de 2022

Comentada:

el 26 de Jul. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by