Make subplot 'stick' to each other

Hi,
I am making a figure with several subplots (maps, specifically). Setting 'Position' will make each subplot very far away from each other since it is setting the outter boundary position. I tried using 'InnerPosition but it seems not working (don't know why, maybe because they are maps?).
How could I make the subplots stick to eachother? My code is also attached below.
fw = 0.5; % figure width
fh = 0.5 ; % figure height
L1 = 0; % row 1 from bottom;
L2 = L1+fh; % row 2 from bottom;
C1 = 0; % column from left
C2 = C1+fw; % column from left
Positions =[C1 L2 fw fh;
C2 L2 fw fh;
C1 L1 fw fh;
C2 L1 fw fh];
cbh = 0.015;
cbw = fw-0.1;
fhi = fw * (latmax-latmin)/(lonmax-lonmin) ; % inner figure height
ver_edge = (0.5-fhi)/2;
cbpos =[C1+0.05 1-0.5*ver_edge cbw cbh;
C2+0.05 1-0.5*ver_edge cbw cbh;
C1+0.05 L1+0.5*ver_edge cbw cbh;
C2+0.05 L1+0.5*ver_edge cbw cbh];
for ps = 1:4
subplot('Position',Positions(ps,:))
% skip other settings
cb1=colorbar('horizontal', 'fontsize',fz, 'fontweight', 'bold','Position',cbpos(ps,:)); % color bar definition; for later use
end

 Respuesta aceptada

Hannah
Hannah el 25 de Abr. de 2022
Okay I see what is the problem. The aspect ratio (figure height / figure width) in worldmap is
((latmax-latmin)/180 )/((lonmax-lonmin)/360)
While what I did is (latmax-latmin)/(lonmax-lonmin). This doubled the aspect ratio therefore leaved lots of white space. Below is the corrected layout I got (without putting any data yet):

2 comentarios

the cyclist
the cyclist el 25 de Abr. de 2022
I'm glad it worked out
Hannah
Hannah el 26 de Abr. de 2022
Thanks for helping!!

Iniciar sesión para comentar.

Más respuestas (1)

the cyclist
the cyclist el 21 de Abr. de 2022
Editada: the cyclist el 21 de Abr. de 2022

0 votos

It might be easier to do what you want with the newer tiledlayout command for making subplots.

4 comentarios

Hannah
Hannah el 22 de Abr. de 2022
Hi,
Thanks! I tried setting 'TileSpacing' to 'none'. But it gives the same layout as what I have now. The space between the top and bottom penel remains. Any solution to that?
the cyclist
the cyclist el 22 de Abr. de 2022
The code you posted errors out because some of the variables are missing. I tried substituting some values in, but I could not replicate anything close to what your figure shows. So, I can't really suggest any fix.
Hannah
Hannah el 22 de Abr. de 2022
Hi there,
Yea I didn't post the entire code since it is super long. I just generated something that can run:
latmin = -45;
latmax = 65;
lonmin = -160;
lonmax = 168;
fw = 0.5; % figure width
fh = 0.5 ; % figure height
L1 = 0; % row 1 from bottom;
L2 = L1+fh; % row 2 from bottom;
C1 = 0; % column from left
C2 = C1+fw; % column from left
Positions =[C1 L2 fw fh;
C2 L2 fw fh;
C1 L1 fw fh;
C2 L1 fw fh];
cbh = 0.015;
cbw = fw-0.1;
fhi = fw * (latmax-latmin)/(lonmax-lonmin) ; % inner figure height
ver_edge = (0.5-fhi)/2;
cbpos =[C1+0.05 1-0.5*ver_edge cbw cbh;
C2+0.05 1-0.5*ver_edge cbw cbh;
C1+0.05 L1+0.5*ver_edge cbw cbh;
C2+0.05 L1+0.5*ver_edge cbw cbh];
fz = 10; % font size
for ps = 1:4
subplot('Position',Positions(ps,:))
worldmap([latmin latmax],[lonmin lonmax]);
setm(gca,'Grid','off','MapProjection','miller','parallellabel','off','meridianlabel','off')
% skip other settings
cb1=colorbar('horizontal', 'fontsize',fz, 'fontweight', 'bold','Position',cbpos(ps,:)); % color bar definition; for later use
end
So I usually adjust 'Position' parameter. But it never allows me to put all the subplots stick to each other. Because my maps are not perfect square, there is always space between the top and bottom panel. Do you see how to remove it?
Hannah
It's definitely an issue with the map. I swapped in a normal plot, and it fills the space.
I don't have any experience with using worldmap, but it could be that a certain aspect ratio is enforced (perhaps because of the particular projection). That makes sense to me, because if you "stretch" only one dimension, you will no long obey the constraints of the projection.
latmin = -45;
latmax = 65;
lonmin = -160;
lonmax = 168;
fw = 0.5; % figure width
fh = 0.5 ; % figure height
L1 = 0; % row 1 from bottom;
L2 = L1+fh; % row 2 from bottom;
C1 = 0; % column from left
C2 = C1+fw; % column from left
Positions =[C1 L2 fw fh;
C2 L2 fw fh;
C1 L1 fw fh;
C2 L1 fw fh];
cbh = 0.015;
cbw = fw-0.1;
fhi = fw * (latmax-latmin)/(lonmax-lonmin) ; % inner figure height
ver_edge = (0.5-fhi)/2;
cbpos =[C1+0.05 1-0.5*ver_edge cbw cbh;
C2+0.05 1-0.5*ver_edge cbw cbh;
C1+0.05 L1+0.5*ver_edge cbw cbh;
C2+0.05 L1+0.5*ver_edge cbw cbh];
fz = 10; % font size
for ps = 1:4
subplot('Position',Positions(ps,:))
plot(rand(5))
% skip other settings
cb1=colorbar('horizontal', 'fontsize',fz, 'fontweight', 'bold','Position',cbpos(ps,:)); % color bar definition; for later use
end

Iniciar sesión para comentar.

Categorías

Productos

Versión

R2019b

Preguntada:

el 21 de Abr. de 2022

Comentada:

el 26 de Abr. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by