View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from

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




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

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

x = 0:0.1:7; y = [sin(x); sin(x+0.2)*0.95];
xlim([1.15 2.45]);

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.

Comment only
25 Feb 2013 Bart van Oort

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


Comment only
19 Feb 2013 Jos (10584)

Jos (10584) (view profile)

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.

Comment only
28 Feb 2013 1.1

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 1.2

fixed typo in description

Contact us