MATLAB Answers

Set position of tick labels

371 views (last 30 days)
Ariel Balter
Ariel Balter on 2 Mar 2011
Answered: JohnA on 3 Nov 2020
Sometimes tick labels end up too close to the axis. Is there a way to adjust the position of the tick labels, for instance, moving the y tick labels a little bit to the left?

  0 Comments

Sign in to comment.

Answers (4)

JohnA
JohnA on 3 Nov 2020
This is a very simple fix that works in Matlab v2019:
% adjust ticklabels away from axes
a=gca;
a.XRuler.TickLabelGapOffset = -8; % negative numbers move the ticklabels down (positive -> up)
a.YRuler.TickLabelGapOffset = -8; % negative numbers move the ticklabels right (negative -> left)
Credit for this solution here:
https://stackoverflow.com/questions/16642404/set-the-position-of-the-xtick-labels-matlab

  0 Comments

Sign in to comment.


Walter Roberson
Walter Roberson on 2 Mar 2011
There is no documented way of doing it.
You could try setting the tick labels manually, to include trailing spaces after the label, something like
set(gca, 'YTickLabel', num2str(reshape(get(gca, 'YTick'),[],1),'%.3f ') )

  2 Comments

Ariel Balter
Ariel Balter on 2 Mar 2011
Thanks. I don't quite get what that is doing, or the following similar answer. But the basic idea seems to be to turn the tick labels into strings and add some space after. Makes sense.
Still no solution for x tick labels.
Very strange to not have this feature.
Walter Roberson
Walter Roberson on 2 Mar 2011
The only solution I know of for xtick is to set xticklabels to [] (the empty array), and then to use the values from the xtick property to figure out where to text() the desired tick labels in to place. With standard font sizes, one line would be 19 pixels high. You have to start out, though, with a conversion between data coordinates and pixels:
xlabshift = 20;
xtickpos = get(gca, 'xtick');
ylimvals = get(gca, 'YLim');
t = text(xtickpos(1), ylimvals(1), str2num(xtickpos(1)));
set(t, 'Units','pixels');
set(t, 'Position', get(t,'Position')-[0 xlabshift 0]);
set(t, 'Units', 'data');
t1 = get(t, 'Position');
xlaby = t1(2);
Then for further labels, instead of setting at y coordinate ylimvals(1) in data space, set at y coordinate xlaby -- it will be the data coordinate corresponding to 20 pixels below the y axis. Unless, of course, you resize the axis...

Sign in to comment.


Matt Fig
Matt Fig on 2 Mar 2011
Not directly, but try this:
YTL = get(gca,'yticklabel');
set(gca,'yticklabel',[YTL,repmat(' ',size(YTL,1),1)])
This simply moves the ticklabels over to the left. As for the x-axis, I don't know how to do that.

  0 Comments

Sign in to comment.


Alejandro Fernández
Alejandro Fernández on 7 Aug 2020
The previous answers doesn't work for me, so I tries with this and that works, it's more os less the previous answer:
ax = gca;
ax.YTickLabel = [num2str(ax.YTick.') repmat(' ',size(ax.YTickLabel,1),1)];

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!