指定部分自动限制时查找实际绘图限制

时间:2015-03-19 20:52:42

标签: matlab matlab-figure

如何在此处找到实际的y范围:

这只是文档http://www.mathworks.com/help/matlab/ref/axis.html

中的示例
 x = linspace(-10,10,200);
 y = sin(4*x)./exp(.1*x);
 plot(x,y)
 axis([-10 10 0 inf])

ymin值指定为零,最大值自动保留。如果我现在用

查询范围
  get(gca,'YLim')

我得到[ 0 inf ]。如何确定使用的实际绘图y范围(对于此示例,它大约为[0 2.5]。)

编辑 - 放在

如果其他人遇到这种情况 - 可能最好避免这个问题:使用全自动范围制作情节,然后根据需要修改范围,以便确切知道它是什么,例如。

  plot(x,y)
  origYrange=ylim
  origXrange=xlim
  axis([origXrange 0 origYrange(2)])

1 个答案:

答案 0 :(得分:1)

虽然文档没有说明,但当ylim的第一个(第二个)值设置为-infinf)时,似乎Matlab会设置较低的(上部)< em> y -axis limit是图中所有 y 值的最小值(最大值)。通过读取轴的所有'YData'的{​​{1}}属性,可以知道后者。

'children'

在您的示例中,结果是

yd = get(get(gca,'children'),'YData'); %// get y data of all plots
if iscell(yd) %// if there's more than one plot yd is a cell array of numeric vectors;
              %// otherwise it's a numeric vector
    yd = [yd{:}]; %// combine all values into a single numeric vector
end
ydminmax = [min(yd) max(yd)]; %// computed limits
result = ylim;
ind = isinf(result);
result(ind) = ydminmax(ind); %// replace infinite values by computed values