
Grouped boxplot plots with different lengths and colors in different categories
10 views (last 30 days)
Show older comments
Hello,
I am currently plotting three different arrays, with different length each, on the same figure using BOXPLOT. Here is currently the code that I am using (sorry for not providing a running example, I am actually loading external data):
% Load the data
x1 = ARRAY1;
x2 = ARRAY2;
x3 = ARRAY3;
% Grouping the data to plot on the same figure
group = [ ones(size(x1));
2 * ones(size(x2));
3 * ones(size(x3))];
% Plotting
% Define the color order
colors = [0 0.4470 0.7410;0.8500 0.3250 0.0980;0.9290 0.6940 0.1250];
% Plot
boxplot(X, group, 'BoxStyle','outline','Colors',colors, 'Symbol', '.');
% Fill the plots according to the color defined
h = findobj(gca,'Tag','Box');
for j=1:length(h)
patch(get(h(j),'XData'),get(h(j),'YData'),get(h(j),'Color'),'FaceAlpha',.3);
end
When running this code, I get the following figure:
I would like to do two things:
- Firstly, all the plots in these figure to be under the same category in the "X" axis.
- Secondly, to add to the same more figure, three more boxes, which are defined by arrays with different length, to the same figure but in another category.
The end result, I would be expecting something like this:

Thanks!
1 Comment
Clay Swackhamer
on 6 Sep 2023
Edited: Clay Swackhamer
on 6 Sep 2023
Regarding your two questions, this code should show how to approach question 1. For question 2, try modifying the line "group" so that it is like this:
group = [1,1,1, 2,2,2, 3,3,3, 4,4,4, 5,5,5,5, 6,6,6,6];
That makes it so there are three data points in group 1, 2, 3, and 4, but four data points in group 5 and 6, and it doesn't create any issue in the plot.
%% box_plot_groups
clear
close all
% Generate example data
group = [1,1,1, 2,2,2, 3,3,3, 4,4,4, 5,5,5, 6,6,6];
for i=1:numel(group)
y(i) = group(i).*rand(1);
end
% Set x axis positions of the groups
positions = [1,1.25,1.5, 2,2.25,2.5];
% Make plot
boxplot(y,group, 'positions', positions, 'Symbol', '', 'Whisker', inf)
% Create axis labels
set(gca,'xtick',[ mean(positions(1:3)), mean(positions(4:end))])
set(gca,'xticklabel',{'Category 1', 'Category 2'})

Accepted Answer
Voss
on 6 Sep 2023
One way:
x1 = randn(10,1);
x2 = randn(10,1);
x3 = randn(10,1);
y1 = randn(100,1);
y2 = randn(50,1);
y3 = randn(20,1);
X = [x1;x2;x3];
Y = [y1;y2;y3];
groupX = [ ones(size(x1));
2 * ones(size(x2));
3 * ones(size(x3))];
groupY = [ ones(size(y1));
2 * ones(size(y2));
3 * ones(size(y3))];
% Define the color order
colors = [0 0.4470 0.7410;0.8500 0.3250 0.0980;0.9290 0.6940 0.1250];
data = {X,Y};
groups = {groupX,groupY};
N_categories = numel(data);
offset = [-0.25, 0, 0.25];
for ii = 1:N_categories
% Plot
h = boxplot(data{ii},groups{ii}, ...
'BoxStyle','outline', ...
'Colors',colors, ...
'Symbol','.', ...
'Widths',0.2, ...
'Positions',ii+offset);
% Fill the plots according to the color defined
for j=1:size(h,2)
patch(get(h(5,j),'XData'),get(h(5,j),'YData'),get(h(5,j),'Color'),'FaceAlpha',.3);
end
hold on
end
xlim([0.5 N_categories+0.5])
xticks(1:N_categories)
xticklabels("Category "+(1:N_categories))
More Answers (0)
See Also
Categories
Find more on Data Distribution Plots 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!