是否可以更改子图的高度?

is it possible to change the height of a subplot?

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

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

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

您可以通过更改索引子图的方式来调整大小。如果您使用 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

输出:

根据需要,您可以通过此获得更多创意,但这应该让您入门。

你应该玩一下 gca 及其 'properties'。一个非常简单的例子是:

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