如何控制子图周围的边距大小?

发布于 2024-10-07 04:54:29 字数 287 浏览 2 评论 0原文

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

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

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

替代文字

I'm plotting 5 x 3 plots using subplot command, but there are massive margins around each subplot.

How do I control the margin size around them?

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

alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

何以畏孤独 2024-10-14 04:54:29

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

The problem is that Matlab assigns the position property of each axis such that there is space around each plot. You can either adjust the position property, or you can get subaxis from the File Exchange and set up the subplots the way you like.

蓝天 2024-10-14 04:54:29

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

Take a look at the axes's LooseInset and OuterPosition properties:
http://undocumentedmatlab.com/blog/axes-looseinset-property/

温柔女人霸气范 2024-10-14 04:54:29

从 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';

Since MATLAB R2019b you can use tiledlayout function to control the spacing of the subplots.


Here's an example which shows how to obtain subplots without tile spacing:

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';
烈酒灼喉 2024-10-14 04:54:29

除了其他答案之外,您还可以尝试 Chad Greene 的 smplot< /a> 来自文件交换。这将产生一个 '小倍数' 图并自动处理 Matlab < 的一些麻烦代码>位置属性。

下面的示例显示了默认的 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

默认 matlab 子图

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

smplot 轴关闭

% 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

smplot 轴打开

In addition to the other answers, you could try Chad Greene's smplot from the FileExchange. This will produce a 'small multiple' plot and automatically deal with some of the hassle of Matlab's position property.

Example below showing default subplot behaviour, smplot with axis off and smplot with axis on, respectively:

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

default matlab subplot

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

smplot with axis off

% 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

smplot with axis on

云醉月微眠 2024-10-14 04:54:29

为了最小化每个子图周围的空白,请运行: [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

此实现自动执行乔纳斯答案中概述的原则。

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

To minimize the white space surrounding each subplot, run: [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

This implementation automates the principle outlined in Jonas's answer.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文