offset of ticks labels

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

 Accepted Answer

dpb
dpb on 9 Jan 2015
Edited: dpb on 24 Feb 2017
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 Comments

Could you be more specific about using text as an option? I didn't get what exactly did you mean. Please give me short example of the source code. Thanks!
dpb
dpb on 9 Jan 2015
Edited: dpb on 9 Jan 2015
Doesn't the above formatting help sufficiently?
Anyway, see enhanced Answer...
mreumi
mreumi on 24 Feb 2017
Edited: mreumi on 24 Feb 2017
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% '))

Sign in to comment.

More Answers (1)

Amos
Amos on 9 Jan 2015
You could try something like
set(gca,'YTickLabel', {'-0.3 ','-0.2 ','-0.1 ','0 ','0.1 ','0.2'})

8 Comments

dpb
dpb on 10 Jan 2015
Edited: dpb on 10 Jan 2015
Nope, that's what I tried earlier -- the renderer strips the trailing blanks and the tick labels don't move...
ERRATUM
OK, I see my testing wasn't thorough enough; I didn't realize the behavior of num2str wasn't to honor a format string in its entirety but that it gets truncated. "That's just rude..." I'll submit a defect report I think. sprintf doesn't but it doesn't handle arrays all that well for the purpose as it returns a single string w/o gyrations to force into proper shape; the whole purpose/need for another function.
Cloud
Cloud on 10 Jan 2015
I tried it and it worked. Do you have an idea how to do it autoimatically for different y-axis ranges? Something like this:
yt = get(gca, 'YTick'); set(gca,'YTickLabel',yt);
but also with the space.
Thanks in advance
dpb
dpb on 10 Jan 2015
Edited: dpb on 10 Jan 2015
Oh, I see I misinterpreted and it's actually num2str instead that isn't saving the trailing blank.
I think it's better to use the text solution anyway unless you're going to use a fixed-width font; otherwise I think you'll have some spacing problems.
But, the question itself -- it's build the array with the trailing blank(s) and pass it. The best you can do I think with automagic formatting is sotoo
set(gca,'yticklab',reshape(sprintf('%-8.1f',ytk),8,[]).')
which uses a left-justified field but that means the decimals don't line up unless you also use the '+' to force the '+' sign which probably don't want for positive label values.(*)
As noted, if it bothered me enough to do it for publishing, I think I'd just use the text solution as it's really not that much more complex and you get the alignment right-justified at the location you pick with whatever format you wish. It also has the advantage in some instances you can use the TeX interpreter (altho if you have R2014+ TMW has finally implemented it there so that is a wash in that case).
() Or, you could add another step of making a blanket substitution for the '+' in the resulting character array via *strrep before the set call. All in all, isn't text simpler, though, in the end?
Thanks a lot. I didn't know how to solve the issue and now i have two solutions. Your suggestion of using 'text' command works perfectly and I also extended the Amnos's solution to following:
yt = get(gca, 'YTick')';
ii=0;
for aa=yt
ii=ii+1;
ylabs{ii}=sprintf('%0.1f ',aa);
end
set(gca,'YTickLabel',ylabs)
(just FYI :) )
Thanks again
If you're going to go that route,
set(gca,'yticklab',arrayfun(@(s) sprintf('%.1f ',s),yt,'uniform',0))
I was trying to keep things less instead of more complex but arrayfun beats using the explicit loop.
If these do solve the issue, "ACCEPT" either or both that do...
Cloud
Cloud on 11 Jan 2015
Edited: Cloud on 11 Jan 2015
Thanks! Since you look pretty well oriented in MatLab, could you please answer my another question:
Because nobody is reacting on it, maybe because nobody knows.
Very clean and simple! thanks a lot..
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);

Sign in to comment.

Products

Asked:

on 9 Jan 2015

Commented:

on 10 Nov 2022

Community Treasure Hunt

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

Start Hunting!