XTick interval with automatic XLim
6 views (last 30 days)
Show older comments
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?
0 Comments
Answers (1)
Walter Roberson
on 3 Mar 2018
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.
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!