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

답변 (3개)

Jan
Jan 2011년 11월 7일

3 개 추천

set(gca, 'YLim', [0, inf]);
Then Matlab uses the hard coded lower limit 0 and the upper limit from the drawn data.

댓글 수: 3

Daniel Shub
Daniel Shub 2011년 11월 7일
This is a little bit different then how I interpreted the question. If you use ylim([-inf, inf]), the y limits are tight to the data in the plot. When you use a YLimMode of auto, the y limits are "rounded" to nice values. I thought that Lara wanted the nice rounding.
Jan
Jan 2011년 11월 7일
@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 2021년 9월 16일
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

댓글을 달려면 로그인하십시오.

Daniel Shub
Daniel Shub 2011년 11월 7일

1 개 추천

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 2011년 11월 7일

0 개 추천

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

카테고리

질문:

2011년 11월 7일

댓글:

2021년 9월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by