How to label a plot in Matlab with combine label?
22 views (last 30 days)
Show older comments
It is hard to explaine here.
For example "peaks" is located second line and combine with "a" and "b".
0 Comments
Accepted Answer
Adam Danz
on 6 Mar 2020
Edited: Adam Danz
on 11 Feb 2021
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 Comments
Adam Danz
on 30 Apr 2020
ax.XAxis.FontAngle = 'italic';
where ax is your axis handle.
Adam Danz
on 1 Jun 2020
Edited: Adam Danz
on 1 Jun 2020
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.
More Answers (1)
Akira Agata
on 6 Mar 2020
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)
See Also
Categories
Find more on Data Exploration in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!