【问题标题】:How can I modify my subplot positions to stop them overwriting each other?如何修改我的子图位置以阻止它们相互覆盖?
【发布时间】:2020-06-08 11:43:52
【问题描述】:

我正在尝试创建一个世界地图的子图(6 个图),我正在将选定的 shapefile 写入其中。我的问题在于子图的位置:它们相互覆盖。我从 stackoverflow 上的其他类似问题中了解到,这是因为轴以某种方式重叠。但我认为我已经创建了它们只是“并排”的职位(见下面的代码)。我试图使轴透明,但这似乎没有帮助。我的问题是:如何修改绘图位置,以免它们相互覆盖?

我正在使用的代码(删除了 shapefile 内容)是:

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 handle subplot axes


    【解决方案1】:

    MATLAB 的 position 向量定义为 [left bottom width height],在您的情况下,如果您查看 h1pos 和 h3pos,它们是

    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 的高度,因此存在重叠,这会导致“子图”删除您的轴。

    要解决此问题,您可以更仔细地计算您的位置,并留出更多空间或降低高度(将高度降低到 0.05 会起作用)。您可以通过执行类似handle6.Position = [0.0300 0.3096 0.4347 0.3157];

    的操作来修改位置属性

    附:您可以考虑通过减少一些冗余来改进您的编码风格。这是一个可以完成这项工作的代码 sn-p

    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
    

    【讨论】:

    • @Zhe 谢谢!正如您所指出的,我需要更仔细地查看宽度和高度。 pos(ii,:) = h.position 语法对我不起作用(2013b 版),但 pos(ii, :) = get(h,'position') 做得很好。
    【解决方案2】:

    subplot 本身将创建不重叠的轴,但会删除任何重叠的现有轴。所以把六个subplot 电话放在第一位,最后改变他们的位置。使用set(handle1,'Position',h1pos),而不是subplot(...) 来改变位置。您还可以使用 axes 创建坐标区对象,而无需删除任何现有的重叠坐标区。由于无论如何您都是手动设置位置,subplot 命令对您没有任何好处。

    您也可以考虑使用新的tiledlayout 功能。

    【讨论】:

    • 感谢@Cris Luengo。我的版本中没有“平铺布局”功能,但之后更改位置的建议很有帮助。
    猜你喜欢
    • 2011-01-19
    • 1970-01-01
    • 2012-01-08
    • 1970-01-01
    • 2021-10-28
    • 1970-01-01
    • 2020-03-13
    • 1970-01-01
    • 2023-02-03
    相关资源
    最近更新 更多