How do I position subplots in MATLAB?

柔情痞子 提交于 2019-12-08 01:59:11

问题


I have problem to set the position of subplots. I'm using subplot inside a loop. But when I try to make special position to the subplots it doesn't work. This is my code:

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

I tried different methods but can't see the third subplot, and sometimes the plot shows only one iteration.

How can I fix this?


回答1:


This create 3 subplots. Position is [left bottom width height]). I usually try to make sure that left + width < 1 and that bottom + height < 1 (for the first subplot).

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])

This works well if you only have 1 column of subplot. For two columns of subplot is use this :

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)
...



回答2:


It is probably happening because of conflicting positions values between the subplot tile number (i.e. subplot(2,2,3) etc) that has its own default position, and the position you entered.

So instead use subplot just with the position info as follows:

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

see also this SO discussion...




回答3:


According to subplot

subplot('Position',[left bottom width height]) creates an axes at the position specified by a four-element vector. The left, bottom, width, and height values are normalized coordinates in the range from 0.0 to 1.0.

Also note that left and bottom values are calculated from the left bottom of the figure.


Here an example of using subplot in a for loop.

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

Hope this helps.



来源:https://stackoverflow.com/questions/16764017/how-do-i-position-subplots-in-matlab

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