MATLAB Answers

Set position of tick labels

조회 수: 445(최근 30일)
Ariel Balter
Ariel Balter 2011년 3월 2일
편집: Walter Roberson 2021년 5월 25일 20:39
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?


JohnA 2020년 11월 3일
편집: Walter Roberson 2021년 5월 25일 20:39
This is a very simple fix that works in Matlab v2019:
% adjust ticklabels away from axes
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:
  댓글 수: 1
Adam Danz
Adam Danz 2021년 5월 25일 19:54
Note, to move the labels away from the axes the offset values should be positive. The comments in the code above are incorrect.
Credit goes to Yair's undocumented Matlab blog rather than the stackoverflow link (which cites Yair's blog).

댓글을 달려면 로그인하십시오.

Walter Roberson
Walter Roberson 2011년 3월 2일
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
Walter Roberson
Walter Roberson 2011년 3월 2일
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...

댓글을 달려면 로그인하십시오.

Matt Fig
Matt Fig 2011년 3월 2일
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.

Alejandro Fernández
Alejandro Fernández 2020년 8월 7일
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)];

Ross Wilkinson
Ross Wilkinson 2021년 5월 25일 17:19
My hacky way of moving xtick labels away from the x axis is to add lines into each label string:
ax = gca;
sp = '\newline\newline\newline\newline\newline'; %5 lines of spacing
ax.XTickLabel = {[sp 'TickLabel1'],[sp 'TickLabel2'],[sp 'TickLabel3']};


Community Treasure Hunt

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

Start Hunting!

Translated by