How to access displayed axis limits when scaling is logarithmic and data partly negative?

3 ビュー (過去 30 日間)
Stephan Heise
Stephan Heise 2012 年 3 月 28 日
Hi,
I plot data. Some of the data are quite noisy and occasionally negative values appear, but I'm not interested in them. I apply a logarithmic y-axis scaling. Matlab issues a warning, that 'Negative data are ignored'. I use axis tight which adjusts the axes limits so that the displayed data (i.e. only the positive data!) just fit into the axes.
Now here's my question: How can I access the displayed y-limits?
get(axes_handle, 'YLim') returns the limits of all the data (positive and negative), so that the upper limit matches the displayed upper limit but the lower limit (negative) does not!
Here's a small example:
figure;
data = [-1 10 100 1000];
plot(data);
set(gca,'YScale','log');
axis tight;
ylim
The displayed y-limits are [10 1000] but ylim returns [-1 1000].
Anyone know a solution?
Stephan.

回答 (2 件)

Matt Tearle
Matt Tearle 2012 年 3 月 28 日
A hack would be to use the YTick values instead, as there's always a tick at the top and the bottom by default.
yl = get(gca,'YTick');
yl = yl([1,end])

C.J. Harris
C.J. Harris 2012 年 3 月 28 日
After you set 'axis tight' call the following commmand:
set(gca, 'YLimMode', 'auto')
Then 'ylim' should return:
[10 1000]
  1 件のコメント
Stephan Heise
Stephan Heise 2012 年 3 月 30 日
Thanks for the hint, Chris. If |YLimMode| is |auto| , |ylim| is indeed returned correctly. A drawback of this workaround is that the axis may not be "tight" after setting |YLimMode| to |auto|.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by