is it possible to change the height of a subplot?

心不动则不痛 提交于 2020-01-07 09:03:08

问题


I have a total of 4 subplots. 1st and 3rd are the actual Signals and 2nd and 4th are their respective clock signals i.e. they are either 0 or 1. The Problem with subplots is that all the plots are of same height. But i want the height of the clock signals to be small compared to the actual signals. and the respective clock signals should be just below their actual signals. I would summarise my requirements:

  1. Reducing the height of the clock signals subplot(i.e the 2nd and the 4th subplot).
  2. Reducing the gap between the first two subplots and the last two subplots.

Anyone who could help me out with this ? Thanks in advance.


回答1:


You should play a little bit with gca and its 'properties'. A very simple example is as:

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




回答2:


You can adjust the size by changing the way that you index the subplots. If you use subplot(4, 1, 1), subplot(4, 1, 2) etc. then they will all have the same height. However, if you use subplot(6, 1, 1:2), subplot(6, 1, 3) etc. then the first subplot will have twice the height of the second.

To adjust the potition between the plots, you can adjust the position property of the axes as follows:

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

Output:

You can get much more creative with this as required, but this should get you started.



来源:https://stackoverflow.com/questions/33690179/is-it-possible-to-change-the-height-of-a-subplot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!