How can I modify my subplot positions to stop them overwriting each other?

a 夏天 提交于 2021-02-05 09:24:06

问题


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 understand from the other questions like this on stackoverflow, that it is because the axes are overlapping somehow. But I thought I had created positions that they would just be 'side-by-side' (see code below). I have tried to make the axes transparent and that doesn't seem to help. My question is: how can I modify the plot positions so they won't overwrite each other?

The code I'm using (with the shapefile stuff removed) is:

clc;
clear all;
%First create the positions for the subplots
handle1=subplot(3,2,1);
H1=get(handle1,'position');
h1pos=H1+[-0.1,-0.1,0.1,0.1]; subplot(2,2,1,'Position',h1pos)
hold all

handle2=subplot(3,2,2);
H2=get(handle2,'position');
h2pos=H2+[-0.1,-0.1,0.1,0.1]; subplot(2,2,1,'Position',h2pos)

handle3=subplot(3,2,3);
H3=get(handle3,'position');
h3pos=H3+[-0.1,-0.1,0.1,0.1]; subplot(2,2,1,'Position',h3pos)

handle4=subplot(3,2,4);
H4=get(handle4,'position');
h4pos=H4+[-0.1,-0.1,0.1,0.1]; subplot(2,2,1,'Position',h4pos)

handle5=subplot(3,2,5);
H5=get(handle5,'position');
h5pos=H5+[-0.1,-0.1,0.1,0.1]; subplot(2,2,1,'Position',h5pos)

handle6=subplot(3,2,6);
H6=get(handle6,'position');
h6pos=H6+[-0.1,-0.1,0.1,0.1]; subplot(2,2,1,'Position',h6pos)

subplot(3,2,1,'Position',h1pos)
text(0.02,0.98,'(a)','Units', 'Normalized', 'VerticalAlignment', 'Top');
%handle1=subplot(2,2,1);
%H1=get(handle1,'position');
%h1pos=H1+[-0.1,-0.1,0.1,0.1]; subplot(2,2,1,'Position',h1pos)
h=worldmap('world')
%     borders('countries', 'Color', 'black')

subplot(3,2,2,'Position',h2pos)
text(0.02,0.98,'(b)','Units', 'Normalized', 'VerticalAlignment', 'Top')
h=worldmap('world')
%     borders('countries', 'Color', 'black') 

subplot(3,2,3,'Position',h3pos)
text(0.02,0.98,'(c)','Units', 'Normalized', 'VerticalAlignment', 'Top')
h=worldmap('world')
%     borders('countries', 'Color', 'black') 

subplot(3,2,4,'Position',h4pos)
text(0.02,0.98,'(d)','Units', 'Normalized', 'VerticalAlignment', 'Top')
h=worldmap('world')
%     borders('countries', 'Color', 'black') 

subplot(3,2,5,'Position',h5pos)
text(0.02,0.98,'(e)','Units', 'Normalized', 'VerticalAlignment', 'Top')
h=worldmap('world')

subplot(3,2,6,'Position',h6pos)
text(0.02,0.98,'(f)','Units', 'Normalized', 'VerticalAlignment', 'Top')
h=worldmap('world')
%     borders('countries', 'Color', 'black') 
%     borders('countries', 'Color', 'black') 

回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/60382789/how-can-i-modify-my-subplot-positions-to-stop-them-overwriting-each-other

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!