【问题标题】:How can I set subplot size in MATLAB figure?如何在 MATLAB 图中设置子图大小?
【发布时间】:2014-06-09 16:59:41
【问题描述】:

我经常需要将 10 个图像一起绘制,但是使用此代码会导致小图像:

img = rand(400,600); 
for i=1:10
 subplot(2,5,i); 
 imshow(img); 
 title(['Image ' int2str(i)]); 
end

如您所见,图像并未使用屏幕中的所有可用空间。如何增加大小,或减少它们之间的填充/边距?

感谢您的帮助。

【问题讨论】:

    标签: matlab plot screen fullscreen figure


    【解决方案1】:

    我不相信有一种简单的方法可以做到这一点。有两种选择:

    首先,使用子图的位置部分:

    >> subplot(2,5, i, [l, b, w, h])
    

    并计算左、下、宽、高。

    或者,获取返回轴的句柄:

    >> h(i) = subplot(2,5,i);
    

    然后再修改轴。

    >> set(h(1), 'position', [l, b, w, h] );
    

    有许多页面会提供更多详细信息,例如,http://www.briandalessandro.com/blog/how-to-make-a-borderless-subplot-of-images-in-matlab/

    [更新]

    下面的代码更详细地说明了哪些人可以执行您正在寻找的事情。这有点乏味。 0.95 和 0.02 只是为了提供一点填充。它们没什么神奇的。 :-)

    要注意的另一件事是,我真的鼓励您使用“ii”作为索引变量(或其他东西),因为“i”被定义为 sqrt(-1)。最好不要使用“i”和“j”作为索引变量(尤其是在 Matlab 中)。

    img = rand(400,600); 
    
    figure(1);
    clf();
    hold on;
    
    % Get the width and height of the figure
    lbwh = get(1, 'position');
    figw = lbwh(3);
    figh = lbwh(4);
    
    % Number of rows and columns of axes
    ncols = 5;
    nrows = 2;
    
    % w and h of each axis in normalized units
    axisw = (1 / ncols) * 0.95
    axish = (1 / nrows) * 0.95
    
    for ii=1:10
    
        % calculate the row and column of the subplot
        row = floor( ii/(ncols+1) ) + 1
        col = mod( ii-1, ncols ) + 1
    
        % calculate the left, bottom coordinate of this subplot
        axisl = (axisw+0.02) * (col-1)
        axisb = (axish+0.02) * (row-1)
    
        %  plot the subplot
        h= subplot('position', [axisl, axisb, axisw, axish] ); 
        imshow(img); 
        title(['Image ' int2str(ii)]); 
    
        pause
    end
    

    您必须使用它才能使其完全按照您的意愿行事。 “帮助”是你的朋友。

    【讨论】:

    • 谢谢!如何获得 l、b、w 和 h?
    • @CengizFrostclaw:我会使用get 来获取当前位置,然后使用[l-(.5*s*w), b-(.5*s*h), w*s, h*s] 之类的东西来扩大s 倍。
    • @CengizFrostclaw:你将不得不对那些给定的图形大小和行数和列数做一些计算。 w = figsizeW / nCols 和 h = figsizeH / nRows。那种东西。
    • @brechmos 我明白了。我想看看这个方法的工作代码。我不知道如何,在哪里调用 get。
    • 谢谢。这很好用,但是它从左下角开始。你需要将 (row-1) 更改为 (2-row)
    【解决方案2】:

    我经常有这个要求,实现它的最有效方法是使用第三方subplot_tight 函数,它或多或少是subplot 的替代品。最简单的你可以做

    figure(1); clf
    subplot_tight(1,2,1, [0.05 0.05])
    %normal plot stuff
    

    第四个参数中的两个参数控制图像周围可见空间的比例。

    【讨论】:

    • 谢谢! +1。我会尽快试试这个
    【解决方案3】:

    根据@brechmos的回答,当你的子图数超过10个子图时,他的代码会触发错误。

     % calculate the row and column of the subplot
     row = floor( ii/(ncols+1) ) + 1
     col = mod( ii-1, ncols ) + 1
    

    例如4X5 单元格,则子图 11 将被错误地解释为 (2, 1),而不是 (3,1)。

    用下面的代码替换它可以修复它:

    % calculate current row and column of the subplot
    row = floor( (i-0.5)/ncols ) + 1;
    col = mod(i-(row-1)*ncols, ncols+1); 
    

    【讨论】:

      【解决方案4】:

      生成绘图后,您可以使用图形属性选项。单击要调整大小的子图。从属性编辑器中选择“更多属性”选项。如果您滚动,您将看到“位置”选项卡。您可以更改这些值以查看子图如何移动,从而根据您的喜好调整子图。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-22
        • 2016-11-25
        • 1970-01-01
        • 2011-11-25
        • 1970-01-01
        相关资源
        最近更新 更多