Different rotations of XLabels

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

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

Thank you, Robert.
The way I originally named the XTicks was by skipping every five year except the last two for TRY and SRY, hence an XTick at these years only.
Can you help me figure out to do the same with the coding you provided? I can only figure out how to skip the years in between manually resulting in an XTick at each year like this:
figure(2)
fh = figure;
ah = axes(fh);
boxplot([cool_1(:,4); cool_1(:,5); cool_1(:,6); cool_1(:,7); cool_1(:,8); ...
cool_1(:,9); cool_1(:,10); cool_1(:,11); cool_1(:,12); cool_1(:,13); ...
cool_1(:,14); cool_1(:,15); cool_1(:,16); cool_1(:,17); cool_1(:,18); ...
cool_1(:,19); cool_1(:,20); cool_1(:,21); cool_1(:,22); cool_1(:,23); ...
cool_1(:,24); cool_1(:,25); cool_1(:,26); cool_1(:,27); cool_1(:,28); ...
cool_1(:,29); cool_1(:,30); cool_1(:,31); cool_1(:,32); cool_1(:,33); cool_1(:,1); cool_SRY(:,13)], ...
group,'symbol', '.','datalim',[0 yc],'extrememode','compress')
h = findobj(gca,'tag','Outliers');
set(h,'MarkerSize',5,'MarkerEdgeColor',[0.5 0.5 0.5])
yline(Q3W_45_1c,':','linewidth',LW)
yline(Med_45_1c)
yline(Q1W,':','linewidth',LW)
xlabel('Year')
ylabel('Cooling need [kWh/m^2 per year]')
% Old tick labels
% set(gca,'XTick',[1 6 11 16 21 26 31 32])
% set(gca,'XTickLabel', {'2021','2026','2031','2036','2041','2046','TRY','SRY'})
% New tick labels
xData = [1:32];
yData = xData;
set(gca,'XTickLabel',[]')
labels = {'2021','','','','','2026','','','','','2031','','','','','2036','','','','','2041','','','','','2046','','','','','TRY','SRY'}';
rotLabels = [zeros(1,30),90,90];
offsetLabel = -6;
for ik = [1 6 11 16 21 26 31 32]
text(ah,'String', labels{ik}, 'Position', [xData(ik) yData(1)+offsetLabel],'VerticalAlignment','middle','HorizontalAlignment','center','Rotation',rotLabels(ik));
end
/Julie
Here's a variation of Robert U's answer that has the following changes
  • Actual XTickLabels are used except for the final two labels. This is a bit more efficient and cleaner, letting the built-in axis properties do a lot of the work.
  • It's very important that the xlim and ylim are set and remain set when using text objects as tick labels. Text objects are in data units so if the axis limits change then the text labels will move with the data!
  • Text objects used as labels copy the axes' font properties. You can add linkprop if you think the axes font properties may change.
x = rand(20,30);
ax = gca();
boxplot(x)
ax.XTick = unique([1:5:size(x,2), size(x,2)-[1,0]]);
ax.XTickLabel = [string(2021:5:2046),"",""];
ax.XTickLabelRotation = 0;
xlim(xlim(ax)) % important (works better than setting X/YLimMode)
ylim(ylim(ax)) % important
t1 = text(ax.XTick(end-1), ...
min(ylim(ax))-range(ylim(ax))*.022, ... %shifted down a bit (2.2% of ylim range)
'TRY',...
'HorizontalAlignment','Right', ...
'VerticalAlignment','Middle',...
'Rotation',90,...
'FontName', ax.FontName, ...
'FontSize', ax.FontSize);
text(ax.XTick(end), ...
t1.Position(2), ...
'SRY',...
'HorizontalAlignment','Right', ...
'VerticalAlignment','Middle',...
'Rotation',90,...
'FontName', ax.FontName, ...
'FontSize', ax.FontSize)
Great, thank you very much, Adam, and also for the explanation!

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!