Customize "XTickLabel" location

23 次查看(过去 30 天)
Rosie
Rosie 2017-8-21
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 个评论
Rosie
Rosie 2017-8-21
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 2017-8-21
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

请先登录,再进行评论。

回答(2 个)

Steven Lord
Steven Lord 2017-8-21
% 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 2018-6-21
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

类别

Help CenterFile Exchange 中查找有关 Grid Lines, Tick Values, and Labels 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by