Create a grouped boxplot
Show older comments
I have 3 groups of data A, B, and C.
A = rand(100,8);
B = rand(100,8)*2;
C = rand(100,8)*4;
I want to plot Box chart of grpup A,B and C.
The internal label of each group is {'a' 'b' 'c' 'd' 'e' 'f' 'g' 'h'}.
data = {A,B,C};
boxplotGroup(data, 'PrimaryLabels', {'a' 'b' 'c' 'd' 'e' 'f' 'g' 'h'}, ...
'SecondaryLabels',{'A', 'B' 'C'}, 'InterGroupSpace', 3)
But I got this error.
Error using assert (line 5)
The number of primary labels must equal either the number of bars per group
(3) or the number of total bars (24).
Error in boxplotGroup (line 155)
assert(ismember(numel(p.Results.primarylabels),[nMembers,
nMembers*nGroups]), ...
Error in (line 13)
boxplotGroup(data, 'PrimaryLabels', {'a' 'b' 'c' 'd' 'e' 'f' 'g' 'h'}, ...
Accepted Answer
More Answers (1)
There are a few ways to accomplish this (or something similar looking) by using boxchart itself. Let's start with the same dummy data as in the original question:
nRows=100;
nCols=8;
nMat=3;
A = rand(nRows,nCols);
B = rand(nRows,nCols)*2;
C = rand(nRows,nCols)*4;
% Stack the data into a single vector:
dataAsVector = [A(:);B(:);C(:)];
% Modify these if the dimensions are changed:
columnLabels = ["a","b","c","d","e","f","g","h"];
matrixLabels = ["A","B","C"];
In the examples below, we'll be "chopping up" the data in dataAsVector, so it might help to have a visual of what it contains. The columns of each matrix are stacked on top of each other, that is, dataAsVector(1:nRows) is the first column of A, and so forth.

Furthermore, it might be helpful to retrace the steps with smaller matrices (i.e. just use two matrices each with two columns) and inspect the inputs to boxchart. In that case, we could use different labels:
columnLabels = strcat("c",string(1:nCols));
matrixLabels = strcat("M",string(1:nMat));
Let's start with creating a single boxchart, where we manually place the columns at locations 1, 2, 3, etc. (of which there are nCols*nMat = 24), using the xgroupdata input:
bSimple = boxchart(categorical(repelem(1:(nCols*nMat),nRows)),dataAsVector);
% Change the labels:
ax = gca;
ax.XTickLabel = repmat(columnLabels,1,nMat);
% Optional: fiddle with manually placing sublabels. This will have to be
% modified if we change the number of columns or number of matrices
ax.XTickLabel([4 12 20]) = {'d\newlineA','d\newlineB','d\newlineC'};
Next, we'll use xgroupdata to distinguish the columns, and GroupByColor to distinguish the matrices. Note that ColorGroupLayout came out in R2025a. The plot below contains three boxchart objects, one per matrix:
figure
b3 = boxchart(categorical(repelem(1:(nCols*nMat),nRows)),dataAsVector,...
'GroupByColor',repelem(matrixLabels,nRows*nCols),'ColorGroupLayout','overlaid');
legend('Location','northeastoutside')
ax = gca;
ax.XTickLabel = repmat(columnLabels,1,nMat);
Next, we'll use xgroupdata to distinguish the matrices, and GroupByColor to distinguish the columns. This creates nCols boxchart objects, i.e. one for each column. Setting the SeriesIndex makes them all have the same color.
figure
b8 = boxchart(repelem(categorical(matrixLabels),nRows*nCols),dataAsVector, ...
'GroupByColor',repmat(repelem(columnLabels,nRows),1,nMat),...
'SeriesIndex',1);
I hope this helps a little bit using boxchart.
Categories
Find more on Labels and Annotations 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!




