Plotting Multiple Box plot in same graph same x axis

I have a data for 5 samples, and for 3 months.
I want to plot 5 box plots for every months. Which means in X-axis I will have January, Mars and April (3 values).
And I want to plot the 5 boxes plot in each month (different color ofc).
I search in the web and unfortunaly I didn't find any case like this, I don't know if it's possible to plot something like that.

 Respuesta aceptada

Maybe something like this?
colors = 'rgbym';
for ii = 1:5
x1 = rand(5,1);
x2 = rand(10,1);
x3 = rand(15,1);
x = ii+[x1; x2; x3];
g1 = repmat({'January'},5,1);
g2 = repmat({'Mars'},10,1);
g3 = repmat({'April'},15,1);
g = [g1; g2; g3];
boxplot(x,g,'BoxStyle','filled','Color',colors(ii));
hold on
end

13 comentarios

Tesla
Tesla el 25 de Feb. de 2022
Thank you for your suggestion.
Yes I want something like that, but for the Y-axis for my data is between 0 and 0.7, Which means some dataset could have same values.
You can see in the previous answer, what it looks like
How about this?
colors = 'rgbym';
for ii = 1:5
x1 = rand(5,1);
x2 = rand(10,1);
x3 = rand(15,1);
x = [x1; x2; x3];
g1 = repmat({'January'},5,1);
g2 = repmat({'March'},10,1);
g3 = repmat({'April'},15,1);
g = [g1; g2; g3];
% b = boxplot(x,g,'BoxStyle','filled','Color',colors(ii));
b = boxplot(x,g,'BoxStyle','filled','Color','k');
set(b(2,:),'Color',colors(ii));
[m,n] = size(b);
for jj = 1:m
for kk = 1:n
set(b(jj,kk),'XData',get(b(jj,kk),'XData')+(ii-3)*0.1);
end
end
hold on
end
Tesla
Tesla el 26 de Feb. de 2022
Thank you a lot,
And how to put my data?
Because the arrays of my data have not the same dimensions.
For example data1 is [0.5 0.78], data2 [0.4 0.6 0.12] data3 [0.9 0.4 0.47]...
Hard to say for sure without knowing what data1, data2, data3, ..., actually represent, but maybe like this? (This uses the same data for all 5 sets of bars, so it's probably not what you have in mind, but maybe you can adapt it to include all your data appropriately.)
data1 = [0.5 0.78];
data2 = [0.4 0.6 0.12];
data3 = [0.9 0.4 0.47];
colors = 'rgbym';
for ii = 1:5
x = [data1(:); data2(:); data3(:)];
g1 = repmat({'January'},numel(data1),1);
g2 = repmat({'March'},numel(data2),1);
g3 = repmat({'April'},numel(data3),1);
g = [g1; g2; g3];
b = boxplot(x,g,'BoxStyle','filled','Color','k');
set(b(2,:),'Color',colors(ii));
[m,n] = size(b);
for jj = 1:m
for kk = 1:n
set(b(jj,kk),'XData',get(b(jj,kk),'XData')+(ii-3)*0.1);
end
end
hold on
end
I got this error
Error in untitled2 (line 30)
b = boxplot(x,g,'BoxStyle','filled','Color','k');
This is the new Code:
data1 = [1.0735];
data2 = [1.0873];
data3 = [1.1093];
data4 = [1.0927];
data5 = [1.1049];
data11 = [1.0735 1];
data22 = [1.0873 0.4];
data33 = [1.1093 0.4];
data44 = [1.0927 0.45];
data55 = [1.1049 0.75];
data111 = [1.0735 1 0.4 0.1];
data222 = [1.0873 0.4 0.45 0.5];
data333 = [1.1093 0.4 0.48 0.99];
data444 = [1.0927 0.45 0.77 0.66];
data555 = [1.1049 0.75 0.11 0.77];
colors = 'rgbym';
for ii = 1:5
x = [data1(:); data2(:); data3(:); data11(:); data22(:); data33(:);data111(:); data222(:); data333(:)];
g1 = repmat({'January'},numel(data1),1);
g2 = repmat({'March'},numel(data2),1);
g3 = repmat({'April'},numel(data3),1);
g = [g1; g2; g3];
b = boxplot(x,g,'BoxStyle','filled','Color','k');
set(b(2,:),'Color',colors(ii));
[m,n] = size(b);
for jj = 1:m
for kk = 1:n
set(b(jj,kk),'XData',get(b(jj,kk),'XData')+(ii-3)*0.1);
end
end
hold on
end
Voss
Voss el 27 de Feb. de 2022
Editada: Voss el 27 de Feb. de 2022
That error is because you put new stuff in x but didn't also put corresponding new stuff in g.
Maybe try it like this (but maybe the data for March (which is what I'm assuming 11, 22, etc., mean) should not include the data for January? and likewise April (111, 222, ...) should not include January and March?):
data1 = [1.0735];
data2 = [1.0873];
data3 = [1.1093];
data4 = [1.0927];
data5 = [1.1049];
data11 = [1.0735 1];
data22 = [1.0873 0.4];
data33 = [1.1093 0.4];
data44 = [1.0927 0.45];
data55 = [1.1049 0.75];
data111 = [1.0735 1 0.4 0.1];
data222 = [1.0873 0.4 0.45 0.5];
data333 = [1.1093 0.4 0.48 0.99];
data444 = [1.0927 0.45 0.77 0.66];
data555 = [1.1049 0.75 0.11 0.77];
all_data = { ...
data1 data2 data3 data4 data5; ...
data11 data22 data33 data44 data55; ...
data111 data222 data333 data444 data555; ...
}
all_data = 3×5 cell array
{[ 1.0735]} {[ 1.0873]} {[ 1.1093]} {[ 1.0927]} {[ 1.1049]} {[ 1.0735 1]} {[ 1.0873 0.4000]} {[ 1.1093 0.4000]} {[ 1.0927 0.4500]} {[ 1.1049 0.7500]} {[1.0735 1 0.4000 0.1000]} {[1.0873 0.4000 0.4500 0.5000]} {[1.1093 0.4000 0.4800 0.9900]} {[1.0927 0.4500 0.7700 0.6600]} {[1.1049 0.7500 0.1100 0.7700]}
n_data = cellfun(@numel,all_data)
n_data = 3×5
1 1 1 1 1 2 2 2 2 2 4 4 4 4 4
colors = 'rgbym';
for ii = 1:5
x = [all_data{:,ii}];
g1 = repmat({'January'},n_data(1,ii),1);
g2 = repmat({'March'},n_data(2,ii),1);
g3 = repmat({'April'},n_data(3,ii),1);
g = [g1; g2; g3];
b = boxplot(x,g,'BoxStyle','filled','Color','k');
set(b(2,:),'Color',colors(ii));
[m,n] = size(b);
for jj = 1:m
for kk = 1:n
set(b(jj,kk),'XData',get(b(jj,kk),'XData')+(ii-3)*0.1);
end
end
hold on
end
Tesla
Tesla el 27 de Feb. de 2022
Thank you very much, that's exactly what I was looking for!
I just have one question, about adding the data points on the graph.
and is it possible to change the box style?
Tesla
Tesla el 27 de Feb. de 2022
Also how to add the legend for this case?
Voss
Voss el 27 de Feb. de 2022
Editada: Voss el 27 de Feb. de 2022
As far as I can tell, there are only two options for 'BoxStyle': 'outline' and 'filled'. Here's what 'outline' would look like in this case:
data1 = [1.0735];
data2 = [1.0873];
data3 = [1.1093];
data4 = [1.0927];
data5 = [1.1049];
data11 = [1.0735 1];
data22 = [1.0873 0.4];
data33 = [1.1093 0.4];
data44 = [1.0927 0.45];
data55 = [1.1049 0.75];
data111 = [1.0735 1 0.4 0.1];
data222 = [1.0873 0.4 0.45 0.5];
data333 = [1.1093 0.4 0.48 0.99];
data444 = [1.0927 0.45 0.77 0.66];
data555 = [1.1049 0.75 0.11 0.77];
all_data = { ...
data1 data2 data3 data4 data5; ...
data11 data22 data33 data44 data55; ...
data111 data222 data333 data444 data555; ...
};
n_data = cellfun(@numel,all_data);
colors = 'rgbym';
for ii = 1:5
x = [all_data{:,ii}];
g1 = repmat({'January'},n_data(1,ii),1);
g2 = repmat({'March'},n_data(2,ii),1);
g3 = repmat({'April'},n_data(3,ii),1);
g = [g1; g2; g3];
b = boxplot(x,g,'BoxStyle','outline','Color',colors(ii));
[m,n] = size(b);
for jj = 1:m
for kk = 1:n
set(b(jj,kk),'XData',get(b(jj,kk),'XData')+(ii-3)*0.1);
end
end
hold on
end
But you can "manually" adjust the width of the bars after they are ceated in boxplot() (EDIT: this also has the few lines of code for creating the legend):
figure()
h_legend = [];
for ii = 1:5
x = [all_data{:,ii}];
g1 = repmat({'January'},n_data(1,ii),1);
g2 = repmat({'March'},n_data(2,ii),1);
g3 = repmat({'April'},n_data(3,ii),1);
g = [g1; g2; g3];
b = boxplot(x,g,'BoxStyle','outline','Color',colors(ii));
h_legend(ii) = b(3,1);
[m,n] = size(b);
for kk = 1:n
% set the width of the bars to 0.12 times the default
for jj = 3:6
set(b(jj,kk),'XData',(get(b(jj,kk),'XData')-kk)*0.12+kk);
end
% shift the boxplots according to the data series (ii)
for jj = 1:m
set(b(jj,kk),'XData',get(b(jj,kk),'XData')+(ii-3)*0.1);
end
end
hold on
end
legend(h_legend,{'1' '2' '3' '4' '5'})
Tesla
Tesla el 27 de Feb. de 2022
Editada: Tesla el 27 de Feb. de 2022
Thank you a lot! you saved my life!
Hope one day I can help you ))
Voss
Voss el 27 de Feb. de 2022
You're welcome!
Can I get a free Tesla?
Tesla
Tesla el 27 de Feb. de 2022
Sure you can have a free Y model ))
Voss
Voss el 27 de Feb. de 2022
Excellent!

Iniciar sesión para comentar.

Más respuestas (1)

Rik
Rik el 25 de Feb. de 2022
Editada: Rik el 25 de Feb. de 2022
You can group them in bins from 1 to 15 and tweak the x labels to match your needs.
See the documentation for how to use 'GroupByColor'.
data=cell(5,3);
for n=1:numel(data)
data{n}=randi(n)*randn(100,1);
end
grouping=cell(size(data));
for n=1:numel(data)
grouping{n}=n*ones(size(data{n}));
end
grouping=[grouping{:}];
data=[data{:}];
boxchart(grouping(:),data(:))
xticks(2.5:5:12.5)
xticklabels({'January','March','April'})

4 comentarios

Tesla
Tesla el 25 de Feb. de 2022
Thank you for your suggestion,
Is it possible to give a color for each data.
For example if data 1 is green, we will see a green boxplot in January and one in March and the other in April.
And so on for the others?
Rik
Rik el 25 de Feb. de 2022
Editada: Rik el 25 de Feb. de 2022
A cursory look at the documentation suggests that this shouldn't be too difficult. Did you try anything?
sample=cell(5,3);
for n_month=1:3
for n_sample=1:5
sample{n_sample,n_month}=n_sample*ones(size(data{n_sample,n_month}));
end
end
sample=[sample{:}];sample=sample(:);
,'GroupByColor',sample
Tesla
Tesla el 26 de Feb. de 2022
Thank you a lot,
And how to put my data?
Because the arrays of my data have not the same dimensions.
For example data1 is [0.5 0.78], data2 [0.4 0.6 0.12] data3 [0.9 0.4 0.47]...
Rik
Rik el 26 de Feb. de 2022
The point is that each variable should be a vector. The best syntax to ensure that depends on your exact data.
Also, if this answer solved the problem, please consider marking it as accepted answer.

Iniciar sesión para comentar.

Categorías

Preguntada:

el 25 de Feb. de 2022

Comentada:

el 27 de Feb. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by