How can I break the text in two or three lines of the x tick label on a box plot?

조회 수: 38 (최근 30일)
Hello everyone! Can anybody help me break the x tick labels in multiple lines?
Each xtick label is actually a really long name so I though it would be better to replaced them with A-K..
I tried using \ or {'A1';'A2'} but it wont work..
boxplot(subjective,'Labels',{'A','B','C','D','E','F','G','H','I','J','K'},...
'PlotStyle','compact','Orientation','vertical', 'BoxStyle','filled','MedianStyle',...
'target', 'Symbol','x','OutlierSize',8);
Thank you in advance!

채택된 답변

dpb
dpb 2021년 8월 29일
Unfortunately, the tick label interpreter won't pass a string with embedded \n on; it instead splits the string into two.
I don't think you can do it with either the 'TeX' or 'LaTeX' interpreter with the tick labels; you would have to use text to write the labels independently.
  댓글 수: 2
Ifigenia Aslanidou
Ifigenia Aslanidou 2021년 8월 30일
Hey, so I did some research and this code was successfull:
h=[];
color1 = gray(60);
boxplot(subjective,'PlotStyle','compact','Orientation','vertical', 'BoxStyle','outline','MedianStyle',...
'target','Color',(color1),'Widths',0.7,'Symbol','x','OutlierSize',8);
ax = gca;
ax.XTick = [1 2 3 4 5 6 7 8 9 10];
ax.XTickLabel = '';
myLabels = {'A1', 'B1', 'C1', 'D1',...
'E1', 'F1', 'G1', 'H1','I1', 'J1';...
'A2','B2','C2', 'D2', 'E2', 'F2',...
'G2','H2','I2', 'J2'};
for i = 1:length(myLabels)
h(i)=text(ax.XTick(i), ax.YLim(1), sprintf('%s\n%s\n', myLabels{:,i}), ...
'horizontalalignment', 'right', 'verticalalignment', 'middle');
set(h,'Rotation',90); %rotating the x label
end
% ax.XLabel.String = sprintf('\n\n\n\n\n%s', 'something'); %x label name
dpb
dpb 2021년 8월 30일
Yes, using text() works ... why TMW neutered the interpreter for the tick labels is anybody's guess.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Axis Labels에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by