why can I not create 3 subplots?

This code deletes the second subplot when the third is created:
clc
figure(1); clf;
sp(1) = subplot(1,3,1, 'Parent', hfig);
set(sp(1),'Position',[0.02 0.05 0.30 0.80]);
sp(2) = subplot(1,3,2, 'Parent', hfig);
set(sp(2),'Position',[0.32 0.15 0.60 0.80]);
sp(3) = subplot(1,3,3, 'Parent', hfig);
set(sp(3),'Position',[0.22 0.25 0.50 0.80]);
But why???

 採用された回答

Matt Tearle
Matt Tearle 2012 年 2 月 21 日

3 投票

subplot deletes any axes that are already where you're trying to put the new one. If you're going to set the position property of the axes manually anyway, why bother with subplot at all? Just use axes:
clc, figure(1); clf;
sp(1) = axes('Parent', hfig);
set(sp(1),'Position',[0.02 0.05 0.30 0.80]);
sp(2) = axes('Parent', hfig);
set(sp(2),'Position',[0.32 0.15 0.60 0.80]);
sp(3) = axes('Parent', hfig);
set(sp(3),'Position',[0.22 0.25 0.50 0.80]);

4 件のコメント

Matthias Pospiech
Matthias Pospiech 2012 年 2 月 21 日
I understand that, whenever I want to plot more than one plot in a single figure I can use subplot, but if I want to adjust the position of each plot I must not use subplot and have to use axes instead ????
Jiro Doke
Jiro Doke 2012 年 2 月 21 日
It's not that you can't adjust the positions of "subplots", but when a subplot is created, it has a behavior of removing any axes that fall underneath. If you look at the help for subplot, the first line says "Create axes in tiled positions". Your example doesn't create a tiled axes. If you want overlapping axes, you should use axes which is the low-level function for creating axes.
BTW, "subplot" and "axes" are related. subplot calls axes inside. It's a convenience function that helps you with easy placement of axes. If you want custom positions, then you would use axes.
Matthias Pospiech
Matthias Pospiech 2012 年 2 月 21 日
I do not want overlapping axis, I just did it to show the effect. Actually I wanted to create 3 plots with equal width side by side. But once I create the third one the second is deleted.
Jiro Doke
Jiro Doke 2012 年 2 月 21 日
Oh I see. The actual axes bounds go beyond the position (since there needs to be space for the axes tick labels), so that's why "subplot" thinks they are overlapping even if you place them side by side.
So you're better off just using the axes function. If you think about it, the first 3 arguments of subplots are meant for axes placement. If you're going to change the placement position, then subplot probably isn't the right choice.

サインインしてコメントする。

その他の回答 (0 件)

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by