如何修改子图位置以阻止它们相互覆盖? [英] How can I modify my subplot positions to stop them overwriting each other?

查看:66
本文介绍了如何修改子图位置以阻止它们相互覆盖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建世界地图的子图(共6个图),并将所选的shapefile写入其中.我的问题与子图的放置有关:它们相互覆盖.我从其他类似stackoverflow的问题中了解到,这是因为轴以某种方式重叠.但是我以为我已经创建了一个职位,它们只是并排"(请参见下面的代码).我试图使轴透明,这似乎无济于事.我的问题是:如何修改绘图位置,以免它们相互覆盖?

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?

我正在使用的代码(除去了shapefile的东西)是:

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') 

推荐答案

MATLAB的 position 向量定义为 [左下宽度高度] ,在这种情况下,如果你看h1pos和h3pos,它们是

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 ,即轴之间的距离小于h1的高度,因此存在重叠,这会导致子图"删除您的轴.

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.

要解决此问题,您可以更仔细地计算位置,并留出更多空间或减小高度(将高度减小到0.05即可).您可以通过执行 handle6.Position = [0.0300 0.3096 0.4347 0.3157];

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.您可以考虑通过减少一些冗余来改善您的编码风格.这是可以完成这项工作的代码段

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

这篇关于如何修改子图位置以阻止它们相互覆盖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆