Efficiently setting lower axis limit

89 ビュー (過去 30 日間)
Omar Mian
Omar Mian 2012 年 4 月 12 日
回答済み: Lucademicus 2019 年 1 月 23 日
I would like to set the lower axis limit and leave the upper limit at the auto value (rather than specifcy the upper limit myself). At the moment I do this to set the lower limit at zero:
xlim_curr = get(gca,'xlim');
xlim_curr(1) = 0;
set(gca,'xlim',xlim_curr)
Is it possible to do this with a single line of code?
Thanks

採用された回答

Jan
Jan 2012 年 4 月 12 日
One line:
set(gca, 'XLim', [0, get(gca, 'XLim') * [0; 1]])
Or without assuming that the lower limit is 0:
set(gca, 'XLim', get(gca, 'XLim') .* [0, 1] + [lowLimit, 0])
But your three lines are easier to read and faster to debug.

その他の回答 (3 件)

onewhaleid
onewhaleid 2018 年 1 月 2 日
編集済み: onewhaleid 2018 年 1 月 2 日
I use the xlim function. When called with no arguments, it returns the current limits:
xlim([0, max(xlim)])

Matt Kindig
Matt Kindig 2012 年 4 月 12 日
You could do it in two lines:
xlim_curr = get(gca,'xlim');
set(gca, 'xlim', [0 xlim_curr(2)]);
I've tried to do exactly this before (fix one axis limit and allow the other to auto-scale), and could not figure out a better way.

Lucademicus
Lucademicus 2019 年 1 月 23 日
From the documentation of r2018b:
'Create a surface plot and show only y values greater than 0. Specify the minimum y-axis limit as 0 and let MATLAB choose the maximum limit.'
[X,Y,Z] = peaks;
surf(X,Y,Z)
ylim([0 inf])

カテゴリ

Help Center および File ExchangeGraphics Object Properties についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by