Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Setting YLim on current axes fails on subplots

303 views
Skip to first unread message

Rob Newman

unread,
May 12, 2010, 4:49:04 PM5/12/10
to
I have an odd problem with setting the YLim values on a plot. I have a figure that consists of three subplots stacked on top of each other. Before I initialize the plotting, I retrieve the data that I am going to plot for each graph, and determine the maximum value from all the datasets. I then use this maximum value to set the YLim values on each subplot, so that the graphs all use the same scale (the maximum value being the highest value in all 3 datasets, and the minimum value is the maximum value multiplied by -1).

This works for the first two of the subplots, but not the bottom (last one). For some reason the y-axis ignores the values I set using set(gca, 'YLim', [ min max ]), which are the same values I have been using for the previous subplots.

If I set the values manual (ie. without first determining the values by parsing the datasets) then the YLim function works fine. So, I feel like the problem is that maybe the type is wrong in the set YLim command? The YLim values are pulled from a vector, and I print them out, and they seem correct. It is most odd.

Here is an example:

% my_y_vals is populated from my data queries:
my_y_vals = [ 1.245 10.345 4.656 ]

% I get the maximum value from this to use as the YLim max and min for each subplot
my_max_y = max(abs(my_y_vals))
my_min_y = my_max_y * -1

figure(1)

% Loop for subplots
for i=1:3,

my_subplot = subplot(3,1,i)

% Bunch of code to get datapoints and then plot them
plot(timevals,data)

% Now set the limits on the Y-axis
set(gca, 'YLim', [my_min_y my_max_y])

% If I switch it out with the following, it works fine
% set(gca, 'YLim', [-20 20])

end

Does anyone have any insight on this? I feel like it is just a simple type casting problem - maybe I need to switch the type to a string or float or something?

Thanks in advance.

Steven Lord

unread,
May 13, 2010, 5:54:56 PM5/13/10
to

"Rob Newman" <rlne...@ucsd.edu> wrote in message
news:hsf480$l10$1...@fred.mathworks.com...

>I have an odd problem with setting the YLim values on a plot. I have a
>figure that consists of three subplots stacked on top of each other. Before
>I initialize the plotting, I retrieve the data that I am going to plot for
>each graph, and determine the maximum value from all the datasets. I then
>use this maximum value to set the YLim values on each subplot, so that the
>graphs all use the same scale (the maximum value being the highest value in
>all 3 datasets, and the minimum value is the maximum value multiplied
>by -1).

*snip*

> % Loop for subplots
> for i=1:3,
>
> my_subplot = subplot(3,1,i)
>
> % Bunch of code to get datapoints and then plot them
> plot(timevals,data)
>
> % Now set the limits on the Y-axis
> set(gca, 'YLim', [my_min_y my_max_y])

Are you certain that gca is the axes you intend to work on? You already
_have_ the handle to the axes -- it's my_subplot. What happens if you use
that in place of gca in your code?

--
Steve Lord
sl...@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ


us

unread,
May 13, 2010, 6:05:19 PM5/13/10
to
"Rob Newman" <rlne...@ucsd.edu> wrote in message <hsf480$l10$1...@fred.mathworks.com>...

a hint:

% replace


set(gca,'YLim',[my_min_y my_max_y])

% with
set(my_subplot,'YLim',[my_min_y my_max_y]);

us

Rob Newman

unread,
May 17, 2010, 4:35:19 PM5/17/10
to
Thanks both of you. I incorporated your suggestions and it appears to work. I wonder why though - I would have thought that gca() was essentially returning the same information??????

Best regards.

Walter Roberson

unread,
May 17, 2010, 4:43:49 PM5/17/10
to
Rob Newman wrote:
> I wonder why though - I would have thought that gca() was
> essentially returning the same information??????

When you are working with multiple axes, such as via subplot(), unless you
have specifically activated a particular axes, you should not assume that it
is active.

ylim works on a per-axes basis, so if you activate it against the wrong axes,
you are not going to get the result you want.

0 new messages