Why do the labels of the last tick marks disappear when I set axes location to origin?

77 views (last 30 days)
The data I'm plotting requires both axes to be at the origin for easy interpretation. For some reason when axes location is set at origin, the labels of the final tick marks disappear. I have no idea how to fix this and I'd rather not resort to moving the axes manually or typing the labels in Illustrator.
Here is the lines of code causing the problem:
ax.XAxisLocation = 'origin'
ax.YAxisLocation = 'origin'
Has anyone encountered this problem before?
  8 Comments
dpb
dpb on 7 May 2017
Quick demo...
hAx=axes;
xlim([-1000 3000])
set(hAx,'xtick',0:1000:2000)
Above reproduces your x axis appearance bestest can do without the 'origin' keyword that is lacking in HG1.
Now if resetting 'xtick' and/or 'xticklabel' won't cause the ticks to reappear as I understand is the situation you're facing,
tvalues=[-1000;3000];
hT=text(tvalues,-0.015*[1;1],num2str(tvalues), ...
'verticalalign','top', ...
'horizontalalign','center');
will write text at the tick label locations.
The coordinates are in data units so you may have to play with the y units a little owing to scaling of the y-axis to other than 0:1 scaling but the 1.5% ratio should get you close.
You can fix this by using relative units in which the spacing is relative to the overall axes position which is then data-scale independent.
As noted, I'd see it Tech Support gives a workaround or points out what I/we are overlooking w/ HG2.

Sign in to comment.

Answers (1)

J. Scott Berg
J. Scott Berg on 2 Jun 2020
If hAx is the axes object, then hAx.YAxis is the NumericRuler object for the Y axis. It has an undocumented property TickLabelChild, which has a String property with the actual strings that are rendered. If you look at hAx.YAxis.TickLabelChild.String, you will find that the label is missing even though it appears in hAx.YAxis.TickLabels. So, you can set up hAx.YAxis.TickLabels how you like it, then do
set(ax.YAxis.TickLabelChild,'String',ax.YAxis.TickLabels)
to have them rendered properly. Do this after you have set the axis limits, tics, etc. If you subsequently do anything that would change axis limits, number of tick labels, etc., first do
set(ax.YAxis.TickLabelChild,'StringMode','auto')
otherwise it may be unhappy about the number of stings in the TickLabelChild not matching up with the number of ticks in the NumericRuler parent.
None of this appears to be documented, so it may not work in your version.

Categories

Find more on 2-D and 3-D Plots 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!