Problem creating axes with subplot

5 views (last 30 days)
RABEHI
RABEHI on 31 Aug 2014
I have a little problem with subplot. Indeed, I try to create 8 axes using the commande subplot, I write this :
subplot(4,2,1,'position',[0.2 0.3 0.05 0.12]);
subplot(4,2,2,'position',[0.3 0.3 0.05 0.12]);
....
until the 8th axes. But, when I run the programme, I get only 6 axes ! the lack is the first and the seconde. Why I have this problme ?
  2 Comments
dpb
dpb on 31 Aug 2014
Don't show all so can't replicate but the odds are you've got overlapping position coordinates which by the doc's
help subplot
...
If a subplot specification causes a new axes to overlap an
existing axes, the existing axes is deleted...
Danylyna Shpakivska
Danylyna Shpakivska on 12 Mar 2023
thanks, that solved the problem!

Sign in to comment.

Answers (3)

Joseph Cheng
Joseph Cheng on 31 Aug 2014
So just like dpb said there is something going on with the subplot and when it is called it may delete the previous axes. Here is an example of what you can do instead of using subplot. Since you're specifying the location using subplot really isn't necessary anymore.
clc
clear all;
Sampimg{1} = rand(100);
Sampimg{2} = repmat(1:100,100,1);
Sampimg{3} = repmat([1:50 50:-1:1],100,1);
Sampimg{4} = repmat([1:50 50:-1:1]',100,1);
Sampimg{5} = rand(100);
Sampimg{6} = repmat(1:100,100,1);
Sampimg{7} = repmat([1:50 50:-1:1],100,1);
Sampimg{8} = repmat([1:50 50:-1:1]',100,1);
subplotMxN = [8];
for Mind = 1:subplotMxN(1)
handles.SubPlot(Mind) = axes;
set(handles.SubPlot(Mind),'Position',[.1+Mind/10 .3 .05 .12])
imagesc(Sampimg{Mind});
end
if you go to another Question asked this week http://www.mathworks.com/matlabcentral/answers/152366-question-about-axes-using-gui-in-matlab I create one that can somewhat do a MxN subplot.
  2 Comments
Image Analyst
Image Analyst on 31 Aug 2014
Joseph, can you adapt your code to put the plots in a 4 row by 2 column array like he tried to do with subplot, instead of all in one row?
Joseph Cheng
Joseph Cheng on 1 Sep 2014
That was done in my previous answered question. Here i was just extrapolating his/her
subplot(4,2,1,'position',[0.2 0.3 0.05 0.12]);
subplot(4,2,2,'position',[0.3 0.3 0.05 0.12]);
by incrementing the x position based on the information i had. The linked version contains something that would try to fit it to their needs. however since i had the time. here we go
clc
clear all;
Sampimg{1} = rand(100);
Sampimg{2} = repmat(1:100,100,1);
Sampimg{3} = repmat([1:50 50:-1:1],100,1);
Sampimg{4} = repmat([1:50 50:-1:1]',100,1);
Sampimg{5} = rand(100);
Sampimg{6} = repmat(1:100,100,1);
Sampimg{7} = repmat([1:50 50:-1:1],100,1);
Sampimg{8} = repmat([1:50 50:-1:1]',100,1);
subplotMxN = [2 4];
width = .05;
height = 0.12;
xSpace = .1;
ySpace = .24;
plotind=1;
for Mind = 0:subplotMxN(1)-1
for Nind = 0:subplotMxN(2)-1
handles.SubPlot(plotind) = axes;
subpos = [0.2+xSpace*Nind 0.3+ySpace*Mind width height];
set(handles.SubPlot(plotind),'Position',subpos)
imagesc(Sampimg{plotind});
plotind = plotind+1;
end
end

Sign in to comment.


Image Analyst
Image Analyst on 31 Aug 2014
Please show all 8 of the calls to subplot. If they're in a loop, then make position a variable and have it print to the command line. Like dpb says, chances are some of the plots overlap and won't get shown. You might use uicontrol() or something to create new axes instead of subplot. Perhaps axes created that way won't have this overlap/not-shown problem - I don't know, I'd have to test. That's (almost) what Joseph's code shows you, except they're all in a line instead of a 4x2 array.

Mike Garrity
Mike Garrity on 4 Sep 2014
One of the features of subplot is that when it creates a new axes it deletes any old ones which the new one would overlap. If you don't want this feature, then you can just call the axes command directly. It won't do this overlap check.
Your code would look something like this:
axes('Position',[0.2 0.3 0.05 0.12]);
axes('Position',[0.3 0.3 0.05 0.12]);
...

Tags

Community Treasure Hunt

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

Start Hunting!