Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top