【问题标题】:is it possible to change the height of a subplot?是否可以更改子图的高度?
【发布时间】:2015-11-13 10:06:05
【问题描述】:

我总共有 4 个子图。第 1 和第 3 是实际信号,第 2 和第 4 是它们各自的时钟信号,即它们是 0 或 1。子图的问题是所有图的高度相同。但我希望时钟信号的高度与实际信号相比要小。并且各自的时钟信号应该刚好低于它们的实际信号。我总结一下我的要求:

  1. 降低时钟信号子图(即第 2 和第 4 子图)的高度。
  2. 缩小前两个子图与后两个子图之间的差距。

谁能帮我解决这个问题? 提前致谢。

【问题讨论】:

  • 要控制子图之间的边距和间隙,请查看 Mathworks 文件交换处的 tight_subplot function。我几乎只将它用于单轴图,以消除轴周围烦人的空白。
  • 我试过这个但没有帮助......它给出了一个错误“未定义的函数'tight_subplot'用于'double'类型的输入参数。”您能否详细说明解决方案...@mikkola

标签: matlab plot subplot


【解决方案1】:

您可以通过更改索引子图的方式来调整大小。如果您使用subplot(4, 1, 1)subplot(4, 1, 2) 等,那么它们将具有相同的高度。但是,如果您使用subplot(6, 1, 1:2)subplot(6, 1, 3) 等,则第一个子图的高度将是第二个的两倍。

要调整绘图之间的药水,您可以调整坐标轴的position 属性,如下所示:

figure
t = 1:0.1:10;

for i = 1:4
    switch i
        case 1
            subplot(6, 1, 1:2)
        case 2
            subplot(6, 1, 3)
        case 3
            subplot(6, 1, 4:5)
        case 4
            subplot(6, 1, 6)
    end

    plot(t, sin(i * t));

    if i == 1 || i == 3
        set(gca, 'xtick', []);

        p = get(gca, 'Position');
        % Increase the height of the first and third subplots by 10%
        p_diff = p(4) * 0.1;
        % Increase the height of the subplot, but this will keep the
        % bottom in the same place
        p(4) = p(4) + p_diff;
        % So also move the subplot down to decrease the gap to the next
        % one.
        p(2) = p(2) - p_diff;
        set(gca, 'Position', p);
    end
end

输出:

您可以根据需要获得更多创意,但这应该可以帮助您入门。

【讨论】:

  • 你是大师。这正是我一直在寻找的(改变相对比例的“rowspan”方法)。
【解决方案2】:

您应该玩一点gca 及其“属性”。一个很简单的例子如下:

clc, clear, close all
x = -2*pi:0.01:2*pi;
y=sin(x);

subplot(2,1,1);plot(x,y);         % plot the first subplot
subplot(2,1,2);plot(x,y,'r');     % plot the second one

A = get(gca,'position');          % gca points at the second one
A(1,4) = A(1,4) / 2;              % reduce the height by half
A(1,2) = A(1,2) + A(1,4);         % change the vertical position
set(gca,'position',A);            % set the values you just changed

【讨论】:

  • 你好。代码工作正常,但如果我添加更多子图,然后增加第一个图的大小,那么它不适合边距。有没有办法增加matlab图形的上边距? @NKN
猜你喜欢
  • 1970-01-01
  • 2023-03-15
  • 2013-08-17
  • 1970-01-01
  • 2020-04-11
  • 1970-01-01
  • 1970-01-01
  • 2013-08-19
  • 1970-01-01
相关资源
最近更新 更多