Customize "XTickLabel" location

21 views (last 30 days)
Rosie
Rosie on 21 Aug 2017
Answered: Jason Kulpe on 21 Jun 2018
I'm using names rather than values for my x-axis using the script below:
set(gca,'XTickLabel',{'P1', 'P2', 'P4', 'P5', 'P6', 'P7', 'P8', 'P9', 'P10','P11', 'P12', 'P13', 'Population Mean'});
However, I want my last XTickLabel to be further to the right so it stands out from the rest of the values. I tried different things like blank ' ' labels or placing extra spaces eg. '<space> Population Mean' but it didn't work. Any suggestions?
Thanks,
Rosie
  3 Comments
Rosie
Rosie on 21 Aug 2017
I actually used spaces before to put extra space between XTixkLabels, but don't know why it doesn't work now.
Walter Roberson
Walter Roberson on 21 Aug 2017
plot(1:20)
ax = gca;
ax.XTick = [1 2 4 5 6 7 8 9 10 11 12 13 14];
ax.XTickLabel = {'P1', 'P2', 'P4', 'P5', 'P6', 'P7', 'P8', 'P9', 'P10','P11', 'P12', 'P13', 'Population Mean'};
pause(3);
ax.XTickLabel = {'P1', 'P2', 'P4', 'P5', 'P6', 'P7', 'P8', 'P9', 'P10','P11', 'P12', 'P13', ' Population Mean'};
In the first version, the 'Population Mean' label will greatly overlap the other labels. Then when it is changed to have a number of spaces before it, you will see it move further right, without having changed the XTick

Sign in to comment.

Answers (2)

Steven Lord
Steven Lord on 21 Aug 2017
% Create a new figure and an axes
figure;
ax = axes;
% Change the X limits of the axes
xlim(ax, [1 15]);
% Change the locations of the tick labels
ax.XTick = [1:5 12];
% Change the tick labels themselves
ax.XTickLabel = {'x1', 'x2', 'x3', 'x4', 'x5', 'the rest'};
Note that I've exaggerated the space between the labels 'x5' and 'the rest'; if you just want a little bit of separation maybe put the labels at [1:2:9 12] instead of [1:5 12].

Jason Kulpe
Jason Kulpe on 21 Jun 2018
I found a way to modify the position of the YAxis for a particular application. Its not exactly what you were looking for but it might help you: here

Community Treasure Hunt

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

Start Hunting!