【问题标题】:How to control the margin size around subplots?如何控制子图周围的边距大小?
【发布时间】:2011-05-23 14:11:00
【问题描述】:

我正在使用 subplot 命令绘制 5 x 3 的图,但是每个子图周围都有很大的边距。

如何控制它们周围的边距大小?

figure;
for c=1:15
    subplot(5,3,c); 
    imagesc(reshape(image(:,c), 360,480)); 
    colormap gray; 
    axis image;
end

【问题讨论】:

标签: matlab plot subplot


【解决方案1】:

问题在于,Matlab 分配了每个轴的 position 属性,以便每个绘图周围都有空间。您可以调整position 属性,也可以从文件交换中获取subaxis 并按照您喜欢的方式设置子图。

【讨论】:

【解决方案2】:

看看坐标区的 LooseInsetOuterPosition 属性: http://undocumentedmatlab.com/blog/axes-looseinset-property/

【讨论】:

    【解决方案3】:

    从 MATLAB R2019b 开始,您可以使用 tiledlayout 函数来控制子图的间距。


    这是一个示例,它显示了如何在没有平铺间距的情况下获取子图:

    figure
    example_image = imread('cameraman.tif');
    t = tiledlayout(5,3);
    nexttile
    
    for c= 1:15
        imagesc(example_image(:,c))
        if c < 15
            nexttile
        end
    end
    
    t.TileSpacing = 'None';
    

    【讨论】:

      【解决方案4】:

      除了其他答案之外,您还可以尝试来自 FileExchange 的 Chad Greene 的 smplot。这将产生一个'small multiple' 绘图并自动处理 Matlab 的position 属性的一些麻烦。

      下面的示例分别显示默认 subplot 行为、smplot 关闭轴和 smplot 轴打开:

      image = randn(360*480,15);
      
      % default subplot
      figure;
      for c=1:15
          subplot(5,3,c); 
          imagesc(reshape(image(:,c), 360,480)); 
          colormap gray; 
          axis image;
      end
      

      % smplot axis off
      figure;
      for c=1:15
          smplot(5,3,c);
          imagesc(reshape(image(:,c), 360,480)); 
          colormap gray; 
          axis off;
      end
      

      % smplot axis on
      figure;
      for c=1:15
          smplot(5,3,c,'axis','on');
          imagesc(reshape(image(:,c), 360,480)); 
          colormap gray; 
          axis tight;
      end
      

      【讨论】:

        【解决方案5】:

        要最小化每个子图周围的空白,请运行:[1]

        for c=1:15
          h_ax = subplot(5,3,c);
          % [...]
        
          outerpos = get(h_ax,'OuterPosition');
          ti = get(h_ax,'TightInset');
          left = outerpos(1) + ti(1);
          bottom = outerpos(2) + ti(2);
          ax_width = outerpos(3) - ti(1) - ti(3);
          ax_height = outerpos(4) - ti(2) - ti(4);
          set(h_ax,'Position',[left bottom ax_width ax_height]);
        end
        

        此实现自动化了 Jonas 回答中概述的原则。

        [1]https://www.mathworks.com/help/releases/R2015b/matlab/creating_plots/save-figure-with-minimal-white-space.html

        【讨论】:

          猜你喜欢
          • 2011-04-14
          • 1970-01-01
          • 2011-10-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-05-03
          • 2011-01-02
          相关资源
          最近更新 更多