Be the first to rate this file! 5 Downloads (last 30 days) File Size: 2.21 KB File ID: #40373

set1limtight

by

 

19 Feb 2013 (Updated )

set ylim tight for the current xlim range of a plot (xlim stays unchanged); incl. option for xlim

| Watch this File

File Information
Description

After setting xlim (or zooming), it is sometimes useful to set ylim to the full range of the ydata WITHIN xlim.
ylim([-inf inf]) will set ylim at min(ydata) to max(ydata)."axis tight" does the same, and also sets xlim([min(xdata) max(xdata)]). In both cases ylim is calculated from the full ydata, and not just the ydata that is visible at the current xlim.

set1limtight('y') sets ylim to min and max of the ydata within range xlim. It leaves xlim unchanged.
set1limtight('x') same for xlim
set1limtight('y',ah) sets ylim for axes handle ah

Example:
x = 0:0.1:7; y = [sin(x); sin(x+0.2)*0.95];
plot(x,y,'o-');
xlim([1.15 2.45]);
set1limtight('y')

MATLAB release MATLAB 7.14 (R2012a)
Other requirements should work in most releases
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (3)
04 Mar 2013 Bart van Oort

Updated according to suggestions by Jos.
Renamed function to set1limtight, replacing setxtight and setytight.

25 Feb 2013 Bart van Oort

Thanks for the suggestions, I will have a look at it later this week.

Bart

19 Feb 2013 Jos (10584)

Nice idea! However, ...
(1) It would be nice to be able to pass axes handles to the function

(2) a more robust approach would be:
function setxtight(ah)
% ... argument/error checks etc. go here ...
L = get(ah,{'ylim','zlim'}) ;
axis(ah,'tight') ;
set(ah,'ylim',L{1},'zlim',L{2}) ;

(3) The help section is broken (did you check what the user sees after typing "help setxtight"

(4) I suggest to combine both set##tight functions in a single submission.

Updates
28 Feb 2013

1) added option to select 'x' or 'y', and to pass an axes handle (passing multiple axes handles is not implemented)
2) added argument/error checks
3) improved help section
4) set1limtight now combines and replaces functions setxtight and setytight

05 Mar 2013

fixed typo in description

Contact us