MATLAB Answers

Different rotations of XLabels

6 views (last 30 days)
Julie Møller
Julie Møller on 21 May 2021
Commented: Julie Møller on 24 May 2021
How can I have the first six XLabels having a rotation of 0 while the last two XLabels get a rotation of 90? Otherwise the last XLabels are not readable.
I have tried splitting the x axis into two lines on top of each other, however I can only find out to construct a two line x axis with labels at the same ticks and not six labels for the first six ticks and two labels for the last ticks.
/Julie

Accepted Answer

Robert U
Robert U on 21 May 2021
Hi Julie,
One way to rotate single XTickLabel Strings is to replace them by text(). Advantage over annotation() is that the text is tied to the actual value of the axes.
fh = figure;
ah = axes(fh);
xData = [3:12];
yData = [1:10];
plot(ah,xData,yData,'o');
ah.XTickLabel = []';
labels = {'2019','2020','2021','2022','2023','2024','2025','2026','TRY','SRY'}';
rotLabels = [zeros(1,8),90,90];
offsetLabel = -0.5;
for ik = 1:length(ah.XTick)
text(ah,'String', labels{ik}, 'Position', [xData(ik) yData(1)+offsetLabel],'VerticalAlignment','middle','HorizontalAlignment','center','Rotation',rotLabels(ik));
end
Kind regards,
Robert
  3 Comments
Julie Møller
Julie Møller on 24 May 2021
Great, thank you very much, Adam, and also for the explanation!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by