在MATLAB的所有三个子图中保持统一的轴限制

时间:2018-10-29 10:18:14

标签: matlab graphics matlab-figure subplot

以下是我编写的代码,其中包含一个for循环中的subplot命令。我希望每个subplot都具有相同的y轴限制,例如0到15。但是,三个图具有不同的(调整后的)y轴限制,这使比较这三个图的真正目的无法实现。如何在所有三个子图中保持相同的ylimit?

w = -pi:0.001:pi;
N = [8 10 14];

for i = 1:3
    subplot(3, 1, i);
    W = exp(-1i*w*(N(i)-1)/2).*sin(w*N(i)/2)./sin(w/2);
    plot(w, abs(W));
    xlabel('\omega'); ylabel('Magnitude');
    title(sprintf('N = %d', N(i)));
end

1 个答案:

答案 0 :(得分:0)

您可以使用linkaxes

w = -pi:0.001:pi;
N = [8 10 14];

ax = NaN(1,3); % preallocate
for i = 1:3
    subplot(3, 1, i);
    ax(i) = gca; % take note of axis handle
    W = exp(-1i*w*(N(i)-1)/2).*sin(w*N(i)/2)./sin(w/2);
    plot(w, abs(W));
    xlabel('\omega'); ylabel('Magnitude');
    title(sprintf('N = %d', N(i)));
end
linkaxes(ax) % link axis limits

作为替代,您可以将轴限制手动设置为所有子图所需的最大尺寸:

w = -pi:0.001:pi;
N = [8 10 14];

ax_size = NaN(3,4); % preallocate
for i = 1:3
    subplot(3, 1, i);
    W = exp(-1i*w*(N(i)-1)/2).*sin(w*N(i)/2)./sin(w/2);
    plot(w, abs(W));
    ax_size(i,:) = axis; % take note of axis size
    xlabel('\omega'); ylabel('Magnitude');
    title(sprintf('N = %d', N(i)));
end
ax_target = [min(ax_size(:,1),[],1) max(ax_size(:,2),[],1) ...
     min(ax_size(:,3),[],1) max(ax_size(:,4),[],1)]; % compute target size
for ii = 1:3
    subplot(3, 1, ii);
    axis(ax_target) % set target size
end