offset of ticks labels

조회 수: 59 (최근 30일)
Cloud
Cloud 2015년 1월 9일
댓글: Jozef Lukac 2022년 11월 10일
Hello,
I am finishing graph to the publication and it looks like this.
The problem is, that y-ticks label are very close to y-axis ("0" is even touching it) and it looks ugly. Is there a way how to set the larger space between tick labels and the axis - set some kind of offset or tell to graph to be smaller then its original size?
Thanks in advance

채택된 답변

dpb
dpb 2015년 1월 9일
편집: dpb 2017년 2월 24일
Not w/o using text instead of letting the axis tick property control them, no.
First fix the formatting issue with tick labels that I've harped on for nearly 30 years -- that of not putting the same number of decimal places on all labels instead of letting integers default. That may fix the problem adequately that you can live with it, but it will certainly go a long way towards the "truly professional" look that I agree is somewhat lacking as default...
ytk=get(gca,'ytick').'; % get the tick values as column vector
set(gca,'yticklabel',num2str(ytk,'%0.1f'))
Ohhh....just thought of a "trick"...let's see--
set(gca,'yticklabel',num2str(ytk,'%0.1f '))
Drat! The renderer strips out the trailing blank so only way to reposition will be via the use of text as mentioned above. Changing positions won't help as the axes labels will just move around together.
ERRATUM While this is quite old, OP did an edit that came up as new activity and in rereading the thread I notice a misstatement that just by chance I discovered the issue in just within the last couple of days at the newsgroup. That is I presumed above that the problem w/ the trailing space in the above was owing to HG; turns out it's a "feature" (I think I think it's a bug, but it's documented behavior) in num2str that it strips blanks even if given a specific format statement so the above doesn't actually pass the string with the trailing blank had assumed it did. One would have to build the string array and append the blank or use sprintf to make the format "stick". END ERRATUM
Oh, I guess you could get more complicated and add a second axes object for the actual display and set the tick labels to empty on the original...but I think just writing text is probably simpler than that.
ADDENDUM
text example code--as above start with retrieving the current tick values, then
set(gca,'yticklab',[]) % clear the labels so don't show
xtik=get(gca,'xtick'); dxtik=xtik(2)-xtik(1); % diff between x ticks
xpos=xtik(1)-0.1*dxtik; % a guess at suitable x location
text(repmat(xpos,size(ytk)),ytk,num2str(ytk,'%0.1f'),'horizontal','right')
The x-position is arbitrary; adjust as desired. I took 10% of the x-axis tick spacing to the left of the first x-tick. You could switch over to absolute positions and remove the dependence on the actual axes values; I'll leave that as "exercise for the student"... :)
  댓글 수: 3
dpb
dpb 2015년 1월 9일
편집: dpb 2015년 1월 9일
Doesn't the above formatting help sufficiently?
Anyway, see enhanced Answer...
mreumi
mreumi 2017년 2월 24일
편집: mreumi 2017년 2월 24일
Works perfectly if you just add a % in front of the space!:
ytk=get(gca,'ytick').'; % get the tick values as column vector
set(gca,'yticklabel',num2str(ytk,'%0.1f% '))

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

추가 답변 (1개)

Amos
Amos 2015년 1월 9일
You could try something like
set(gca,'YTickLabel', {'-0.3 ','-0.2 ','-0.1 ','0 ','0.1 ','0.2'})
  댓글 수: 8
Shreejit Jadhav
Shreejit Jadhav 2021년 5월 21일
Very clean and simple! thanks a lot..
Jozef Lukac
Jozef Lukac 2022년 11월 10일
To adjust a space in tick label you can use latex:
set(gca,'TickLabelInterpreter', 'latex');
and then use \vspace latex command, e.g. '\vspace{5pt}' or another latex positioning command in the tick label (instead of a plain space):
yTicks_vect = -0.3:0.1:0.2;
set(gca,'YTick', yTicks_vect);
addStr = '\hspace{5pt}';
cellArr_yLabels = cellfun(@(x) ...
[sprintf('%.1f',x), addStr], num2cell(yTicks_vect),...
'UniformOutput',false);
set(gca,'YTickLabel', cellArr_yLabels);

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

카테고리

Help CenterFile Exchange에서 Axis Labels에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by