About Ylim

14 views (last 30 days)
Lara
Lara on 7 Nov 2011
Commented: Marcel on 16 Sep 2021
Hello everyone ! I want to plot a curve with positive and negative values on the Y-axis. I want to limit the figure to positive values so Ylim starts from 0, but I also want the high limit to be in mode auto. Is there a way to do so ? Thanks for your replies

Answers (3)

Jan
Jan on 7 Nov 2011
set(gca, 'YLim', [0, inf]);
Then Matlab uses the hard coded lower limit 0 and the upper limit from the drawn data.
  3 Comments
Jan
Jan on 7 Nov 2011
@Daniel: Correct. Using Grzegorz' method considers the autoscaling. But the auto-scaling is affected by setting the lower limit recursively, see:
x = rand(1, 100) - 0.9; subplot(1,2,1); plot(x); subplot(1,2,2); plot(max(0, x));
Is there a public description of the auto-limit algorithm?
Marcel
Marcel on 16 Sep 2021
is there any new knowledge about the question, how the auto-limit algorithm looks like?
for performance reasons I try to avoid the "auto" mode, I believe a self coded function can be way faster... THANKS

Sign in to comment.


Daniel Shub
Daniel Shub on 7 Nov 2011
I set the YLimMode to auto in case you have already specified a ylim value. If the max of ylim is less than 0, you need to set the ylim to something else (in this case [0, 1]).
set(gca, 'YLimMode', 'Auto');
set(gca, 'YLim', [0, max([1, max(ylim)])]);

Grzegorz Knor
Grzegorz Knor on 7 Nov 2011
Get ylim value from current axis, and then set limit from zero to maximum (second) value:
plot(randn(1000,1))
yl = get(gca,'ylim');
ylim([0 yl(2)])

Community Treasure Hunt

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

Start Hunting!