XTick interval with automatic XLim

8 次查看(过去 30 天)
jg
jg 2018-3-3
编辑: jg 2018-3-3
I have a situation where I want to specify the interval between the XTick values, but I also want the limits (XLim) to be automatic (or be partially automatic, as in the case of XLim = [0 inf]).
For example, let's say I want my XTick increments to be 0.5, regardless of what my x limits are:
h.XTick = 0 : 0.5 : 10 ;
h.XLimMode = 'auto';
If my plotted data ends up having x limits from 0-20 instead of 0-10, then my plot won't have tick labels between 10-20. I could guess what my final limits will be and make XTick larger than that (since XTick values outside the bounds of XLim aren't drawn), but that still requires having an idea of what XLim will be.
Is this possible to do without manually resetting XTick every time the plot changes?

回答(1 个)

Walter Roberson
Walter Roberson 2018-3-3
You can proceed by two routes:
1) Add a ResizeFcn callback at the figure level. Each time it is fired, have it locate the axes of interest, ax, and
XL = get(ax, 'XLim');
set(ax, 'XTick', XL(1):0.5:XL(2);
2) Or, use
el = addlistener(ax, 'XLim', 'PostSet', @(varargin) set_xtick(varargin{:}) );
where set_xtick is a function to do pretty much the same code to an axes that is passed as its first parameter.
There are some subtle points about when the listener will be destroyed so you should read the addlistner documentation.
  1 个评论
jg
jg 2018-3-3
编辑:jg 2018-3-3
(1) doesn't work for me because I don't want it to depend on the figure resizing.
The above link demonstrates a solution for this problem, but it makes no sense to me - it looks like it's manually triggering a callback, as one commenter points out.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by