How do I fix this bug: xticklabel is not aligned with the xtick position.

3 views (last 30 days)
I've been using Matlab for years and have only just come across this. Thankfully it's pretty simple to demonstrate. I get a weird bug when I run:
xAxis = -.25:.01:.25;
figure;plot(xAxis,rand(1,length(xAxis)));
set(gca,'xtick',-.1:.1:.1,'xticklabel',-100:100:100);
The central '0' label is offset from the position of the xtick for 0 (it isn't aligned).
I've run the code on 2014b, 2015b and 2016a and get the same result (all run on a Mac).
I imagine it's something to do with the way that Matlab aligns the string labels - it doesn't happen if the labels are all 0s for example. However it seems like quite a basic thing to get wrong...
Is there a fix for this bug or a quick workaround?
Thanks, Chris
  1 Comment
Chris Scholes
Chris Scholes on 20 Jun 2016
Edited: the cyclist on 20 Jun 2016
So... I found the workaround myself if anyone has the same problem. Put the labels in as strings:
xAxis = -.25:.01:.25;
figure;plot(xAxis,rand(1,length(xAxis)));
set(gca,'xtick',-.1:.1:.1,'xticklabel',{'-100','0','100});

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!