readjusting Y limits on subplots

I am plotting for subplots (2,2,i) on a figure and want all the Y'axis to be the same. As I'm plotting them sequentially, I will only know all the max Y values once all 4 are plotted.
Can I go back, work out the max Y limit and then reapply that to the other plots?
Thanks Jason

Answers (3)

Do your plots and store the gca for each plot
for i = 1:3
subplot(1, 3, i)
plot(10*hist(rand(1, 100))); % some random data
mygca(i) = gca;
end
Get the ylimits of each subplot
yl = cell2mat(get(mygca, 'Ylim'))
Compute new limits for each subplot
ylnew = [min(yl(:,1)) max(yl(:,2))];
set(mygca, 'Ylim', ylnew)
The nice thing is that you can use 'set' with a vector of gca's as argument.

9 Comments

This is really great. One question, as I want to remove the exponent notation, why does:
set(mygca, 'YTickLabel', num2str(get(gca, 'YTick')'),'FontSize',8)
only act upon the last drawn subplot??
my mistake, should be:
set(mygca, 'YTickLabel', num2str(get( my gca, 'YTick')'),'FontSize',8)
Please accept the answer if you are happy with it.
hmm, I thought I had it but this doesn't work:
Error in num2str (line 66)
xmax = double(max(abs(widthCopy(:))));
Error in Montage>pushbuttonCH_Callback (line 2699)
set(mygca, 'YTickLabel', num2str(get(mygca, 'YTick')'),'FontSize',8)
You have too many quotation marks marks in that last line in the num2str call. Get rid of the trailing one at the end.
Thats a transpose!
Ah, sorry, I just glanced at it and thought it looked wrong!
It seems like that approach won't change the 4 yaxis together to remove the exponent notation :-(
Well, that's a different question!

Sign in to comment.

Adam
Adam on 24 Nov 2014
'Yes'
would be the answer to your question. I assume you are asking how to do it though? There are numerous ways.
You could keep track of the maxima of your data as you plot it and then just set the 'YLim' property of all of the axes appropriately, you can plot them all, then get the 'YLim' property from them all, find the largest and apply that to them all or variations on the same theme, assuming you keep track of your axes handles.

9 Comments

Jason
Jason on 24 Nov 2014
Edited: Jason on 24 Nov 2014
Im using
yy1 = get(gca,'YLim')
Then once all plotted..
set(h1,'YLim',yy1);
set(h2,'YLim',yy1);
set(h3,'YLim',yy1);
set(h4,'YLim',yy1);
(where
h1=subplot(2,2,1)
h2=subplot(2,2,2)
h3=subplot(2,2,3)
h4=subplot(2,2,4)
But its not doing it correctly
It's hard to see what could possibly be going wrong there if those set instructions are literally all in a row with nothing changing yy1 between their calls.
I'm not sure what 'gca' will be at the time you call it though. Nevertheless, even if that is wrong, your instructions should still set all axes' y limits to be the same.
I am using bar charts as my plots (as Im plotting histograms)
i.e.
h1=subplot(2,2,1)
bar(x1,y1)
yy1=get(gca,'YLim')
h2=subplot(2,2,2)
bar(x2,y2)
yy2=get(gca,'YLim')
h3=subplot(2,2,3)
bar(x3,y3)
yy3=get(gca,'YLim')
etc..
when I view yy1, yy2 etc at the end, they are correctly giving the limits. so it appears to be setting thats not working.
you can try using linkprop or linkaxes, but to be honest what you are doing should work fine. When I try setting the YLim property of an axes containing a bar plot it works fine so I'm a bit stumped as to why yours does not
Jason
Jason on 24 Nov 2014
Edited: Jason on 24 Nov 2014
Actually, maybe it is but the yaxis labels aren't updating!
Very strange
Unless you explicitly set the label mode to 'manual' they should also update automatically when you change the limits.
Jason
Jason on 24 Nov 2014
Edited: Jason on 24 Nov 2014
I dont think I do, but I do set so to not display in exponent notation: I do this after each subplot
set(gca, 'YTickLabel', num2str(get(gca, 'YTick')'),'FontSize',8)
That would explain it then, setting your own tick labels automatically sets the mode to Manual and it will therefore not update in response to changes in the actual y limits.
If you are going to over-ride the auto tick labels you will have to keep doing so every time the y limits change.
You are setting each one to the same thing that it already was by using num2str( get( mygca, 'YTick' ) ).
You need to get the ticks from the one that is correct and create labels from that and then set those across all, or just for each axes do whatever you did in the first place to change the notation from exponential every time you change y limits.
There is a 'PostSet' event that you can add a listener to I think, though I have never used it on graphics objects, only my own objects.
I think though you can do something like:
addlistener( hAxes, 'XLim', 'PostSet', callback )
You may be better using linkprop after all on the 'YTick' and 'YTickLabel' properties. Link all your axes together and then just update the YTick and YTickLabel of the first axes and the rest will follow.

Sign in to comment.

Another approach is to use linkaxes to link the y-axis limits of all subplots. When combined with axis tight, that y-axis range of all subplots will equal the largest range of data in all plots.
clf()
ax = gobjects(9,1);
for i = 1:9
ax(i) = subplot(3,3,i);
plot(randi(randi(1000),1,20), 'o');
axis(ax(i),'tight')
end
linkaxes(ax, 'y')

Asked:

on 24 Nov 2014

Answered:

on 15 Apr 2021

Community Treasure Hunt

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

Start Hunting!