File Exchange

image thumbnail


version 1.2 (2.21 KB) by

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



View License

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]);

Comments and Ratings (3)

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

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


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.



fixed typo in description


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

MATLAB Release
MATLAB 7.14 (R2012a)

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

» Watch video