How to label a plot in Matlab with combine label?

26 ビュー (過去 30 日間)
Matlaber 2020 年 3 月 6 日

It is hard to explaine here.
For example "peaks" is located second line and combine with "a" and "b".

サインインしてコメントする。

採用された回答

Adam Danz 2020 年 3 月 6 日

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
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 件のコメント26 件の古いコメントを表示26 件の古いコメントを非表示
Adam Danz 2020 年 4 月 30 日
ax.XAxis.FontAngle = 'italic';
where ax is your axis handle.
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 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 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Matlaber 2020 年 3 月 7 日
Thanks. Is there a away to combine both boxplot together?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAxis Labels についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by