I am trying to create a subplot (6 plots) of world maps into which I am writing chosen shapefiles. My issue is with my placement of subplots: They are overwriting each other. I
subplot
by itself will create axes that don't overlap, but will delete any existing axes that do overlap. So put the six subplot
calls first, and change their positions at the end. Use set(handle1,'Position',h1pos)
, not subplot(...)
to change the position. You can also use axes
to create an axes object without deleting any existing overlapping axes. Since you're setting the position manually anyway, the subplot
command has no advantage for you.
You could also consider using the new tiledlayout functionality.
MATLAB's position
vector is defined as [left bottom width height]
, and in your case, if you look at h1pos and h3pos, they are
h1pos = [0.0300 0.6093 0.4347 0.3157]
h3pos = [0.0300 0.3096 0.4347 0.3157]
h1pos(2) - h3pos(2) = 0.2996 < 0.3157
, i.e. the distance between the axes is smaller than the height of your h1, and as a result there is a overlap, which leads to "subplot" deleting your axes.
To solve this, you can calculate your positions more carefully and either leave more space or reduce the height (by reducing the height to 0.05 would work). You can modify the position property just by doing something like handle6.Position = [0.0300 0.3096 0.4347 0.3157];
P.S. you could consider improve your coding style by reducing some redundancy. Here is a code snippet that would do the job
offset = [-0.05,-0.05,0.1,0.05];
pos = zeros(6, 4);
for ii = 1:6
h = subplot(3,2,ii);
pos(ii, :) = h.Position;
end
for ii = 1:6
subplot('Position',pos(ii,:) + offset);
text(0.02,0.98,['(' char('a'+ii-1) ')'],'Units', 'Normalized', 'VerticalAlignment', 'Top');
h=worldmap('world');
end