MATLAB Multiple box plots in single figure

I tried using the code below a copy of your answer to creating multiple boxplots in a single figure @ Luis Mendo. Unfortunately, I have not gotten it to work for my data.

This is the final plot that needs to be created. enter image description here

Below is the code to be modified:

TN = [0.08  0.07    0.10  0.04  0.04    0.07  0.04  0.04    0.05    0.03    0.03    0.03  0.03  0.03    0.04
0.11    0.11    0.11  0.06  0.05    0.04  0.03  0.03    0.03    0.03    0.03    0.03   0.03 0.04    0.03
0.09    0.05    0.08  0.03  0.03    0.03  0.03  0.03    0.03    0.03    0.03    0.03   0.03 0.03    0.03
0.11    0.10    0.14  0.04  0.05    0.08  0.03  0.03    0.03    0.03    0.03    0.03   0.03 0.03    0.03
0.11    0.11    0.14  0.09  0.05    0.07  0.06  0.03    0.03    0.03    0.03    0.03   0.03 0.03    0.03]; 


Depth = {'0-2"' '2-4"' '4-6"' '8-10"' '10-12"'};
PlotColors = {'r' 'g' 'c'};
Depth1 = TN(1:5,1:3);
Depth2 = TN(1:5,4:6);
Depth3 = TN(1:end,7:9);
Depth4 = TN(1:end,10:12);
Depth5 = TN(1:end,13:15);


GroupedData = {Depth1 Depth2 Depth3 Depth4 Depth5}; 

legendEntries = {'Summer 2019' 'Fall 2019' 'Summer 2020'};

N = numel(GroupedData);
delta = linspace(-.3,.3,N); %// define offsets to distinguish plots
width = .2; %// small width to avoid overlap
cmap = hsv(N); %// colormap
legWidth = 1.8; %// make room for legend

figure;
hold on;
for ii=1:N %// better not to shadow i (imaginary unit)
%if ii~=ceil(N/2)
%    labels = repmat({''},1,N); %// empty labels
%else
    labels = Depth; %// center plot: use real labels
%end
boxplot(GroupedData{ii},'Color', PlotColors{ii}, 'boxstyle','filled', ...
    'position',(1:numel(labels))+delta(ii), 'widths',width, 'labels',labels)
    %// plot filled boxes with specified positions, widths, labels
plot(NaN,1,'color',PlotColors{ii}); %// dummy plot for legend
end
xlabel('Depth'); ylabel('TN'); grid on;
xlim([1+2*delta(1) numel(labels)+legWidth+2*delta(N)]) %// adjust x limits, with room for legend

legend(legendEntries);


Read more here: https://stackoverflow.com/questions/65724397/matlab-multiple-box-plots-in-single-figure

Content Attribution

This content was originally published by Zak at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: