如何在MATLAB中定位子图?

时间:2013-05-26 21:51:37

标签: matlab loops subplot

设置子图的位置有问题。我在循环中使用子图。但是当我试图对子图进行特殊定位时,它不起作用。这是我的代码:

h=subplot(2,2,3);
set(h,'position',[0.15 0.15 0.4 0.4]);
plot(d3,S3,'*','Color',colors(i,:));

我尝试了不同的方法,但看不到第三个子图,有时情节只显示一次迭代。

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:3)

可能是因为具有自己的默认位置的子图块区号(即subplot(2,2,3)等)与您输入的位置之间存在冲突的位置值。

因此,只需使用位置信息的子图,如下所示:

subplot('position', [0.15 0.15 0.4 0.4])
plot(d3,S3,'*','Color',colors(i,:));
subplot('position', [... ... ... ...])
plot(...);

另见this SO discussion ...

答案 1 :(得分:2)

根据subplot

子图('位置',[左下宽度高度])在由四元素矢量指定的位置处创建轴。左,下,宽度和高度值为标准化坐标,范围为0.0到1.0。

另请注意,左下角的值是从图的左下角计算的。


这是在for循环中使用子图的示例。

figure

% subplot dimension
n1 = 2; % number of rows
n2 = 3; % number of columns

% These values would define the space between the graphs
% if equal to 1 there will be no space between graphs
nw = 0.9; % normalized width
nh = 0.9; % normalized height

for k1 = 1:n1
    for k2 = 1:n2
        subplot(n1,n2,(k1-1)*n2 + k2,...
            'position', [(1-nw)/n2/2 + (k2-1)/n2, (1-nh)/n1/2 + 1-k1/n1,...
            nw/n2 nh/n1]);
        % plot something
        plot(rand(5));
        % turn off the labels if you want
        set(gca, 'XTick', []);
        set(gca, 'YTick', []);
    end
end

希望这有帮助。

答案 2 :(得分:2)

这创建了3个子图。位置是[左下宽度高度])。我通常会尝试确保左+宽度< 1和那个底部+高度&lt; 1(对于第一个子图)。

figure
set(subplot(3,1,1), 'Position', [0.05, 0.69, 0.92, 0.27])
set(subplot(3,1,2), 'Position', [0.05, 0.37, 0.92, 0.27])
set(subplot(3,1,3), 'Position', [0.05, 0.05, 0.92, 0.27])

如果您只有1列子图,则效果很好。对于两列子图,请使用:

figure
subplot(4,2,1)
plot(...)
set(gca, 'OuterPosition', [0, 0.76, 0.49, 0.23])
subplot(4,2,2)
plot(...)
set(gca, 'OuterPosition', [0.48, 0.76, 0.49, 0.23])
subplot(4,2,3)
...