How to label a plot in Matlab with combine label?

조회 수: 21 (최근 30일)
Matlaber
Matlaber 2020년 3월 6일
편집: Adam Danz 2021년 2월 11일
It is hard to explaine here.
For example "peaks" is located second line and combine with "a" and "b".

채택된 답변

Adam Danz
Adam Danz 2020년 3월 6일
편집: Adam Danz 2021년 2월 11일
Option 1: use boxplotGroup()
See boxplotGroup() on the file exchange. If your boxplot data are matrices with the same number of columns, you can use this function to group them and provide primary and secondary labels.
Option 2: Use newline to create two rows of tick labels
Pro: Easy and clean
Con: Less control of the alignment between the upper and lower labels.
ax = axes();
xlim([0,12])
ticks = {'a_peaks','b','a_concat','b','a_F0','b','a_Fs','b'};
ticks = strrep(ticks,'_','\newline'); % replace underscore with \newline
ax.XTick = [1 2 4 5 7 8 10 11];
ax.XTickLabel = ticks;
This variation below controls the alignment between the upper and lower labels but adds an additional tick between the upper labels.
ax = axes();
xlim([0,12])
ticks = {'a','_peaks','b','a','_concat','b','a','_F0','b','a','_Fs','b'};
ticks = strrep(ticks,'_','\newline'); % replace underscore with \newline
ax.XTick = [1 1.5 2 4 4.5 5 7 7.5 8 10 10.5 11];
ax.XTickLabel = ticks;
Option 2.1: Use cell array with justified text
Added to answer 11-Feb-2021
Pro: Intuitive label arrangement & text justification
ax = axes();
xlim([0,12])
ticks = {'a','','b','a','','b','a','','b','a','','b'; ... % 1st row of labels
'','peaks','','','concat','','','F0','','','Fs',''}; % 2nd row of labels
ticksJust = strjust(pad(ticks),'center'); % Justify: 'left'(default)|'right'|'center
% ticksJust =
% 2×9 cell array
% {' a '} {' '} {' b '} {' a '} {' '} {' b '} {' a '} {' '} {' b '}
% {' '} {'peaks'} {' '} {' '} {'peaks'} {' '} {' '} {'peaks'} {' '}
tickLabels = strtrim(sprintf('%s\\newline%s\n', ticksJust{:}));
ax.XTick = [1 1.5 2 4 4.5 5 7 7.5 8 10 10.5 11];
ax.XTickLabel = tickLabels;
Option 3: Use text() to create a second row of tick lables
Pro: More control over the placement of the tick labels.
Con: Axis limits must be set and the lower tick labels will move if the pan feature is used.
ax = axes();
% Set axis limits before labeling
xlim([0,12])
ylim([0,1]);
% Shorten the height of the axes to 90% and shift
% upward to make room for the 2-layers of labels.
upperPosition = sum(ax.Position([2,4]));
ax.Position(4) = ax.Position(4) * .9;
ax.Position(2) = minus(upperPosition, ax.Position(4));
% label the upper row using xtick and xticklabel
ax.XTick = [1 2 4 5 7 8 10 11];
ax.XTickLabel = {'a' 'b'};
% use text() to creat the lower row of tick labels
% The 2nd layer will be 6% of the y axis range below the axis.
lowerLevel = min(ylim(ax))-range(ylim(ax))*.06;
lowerLabels = {'peaks','concat','F0','FS'};
text([1.5, 4.5, 7.5, 10.5], repmat(lowerLevel,1,numel(lowerLabels)), lowerLabels,...
'VerticalAlignment','Top','HorizontalAlignment','Center')
  댓글 수: 28
Adam Danz
Adam Danz 2020년 4월 30일
ax.XAxis.FontAngle = 'italic';
where ax is your axis handle.
Adam Danz
Adam Danz 2020년 6월 1일
편집: Adam Danz 2020년 6월 1일
If you want 2 groups with 3 boxplots per group, your input will be a 1x3 cell array.
Each of the 3 cells will should contain 2 columns where column 1 are the data for the first boxplot of each group, column 2 are the data for the 2nd boxplot for each group
splitdata = {c(:,[1,4]),c(:,[2,5]),c(:,[3,6])};
Please let me know if you have any problems or need more clarification.

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

추가 답변 (1개)

Akira Agata
Akira Agata 2020년 3월 6일
I believe the simple way is to separate the plot for each group and use xlabel to add 'Peaks', 'Concat', etc.
The following is an example:
figure
subplot(1,2,1)
boxplot(rand(100,2),{'a','b'})
xlabel('Peaks','FontSize',12)
subplot(1,2,2)
boxplot(rand(100,2),{'a','b'})
xlabel('Concat','FontSize',12)
  댓글 수: 1
Matlaber
Matlaber 2020년 3월 7일
Thanks. Is there a away to combine both boxplot together?

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

카테고리

Help CenterFile Exchange에서 Data Exploration에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by