offset of ticks labels
Show older comments
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
More Answers (1)
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
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
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?
Cloud
on 10 Jan 2015
dpb
on 10 Jan 2015
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...
Shreejit Jadhav
on 21 May 2021
Very clean and simple! thanks a lot..
Jozef Lukac
on 10 Nov 2022
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);
Categories
Find more on Axis Labels in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!