Drawing fancy box plots
76 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
omid zandi
el 7 de Mayo de 2021
Hi
is it possible to draw boxplots same as what i attached, in matlab? I have tried toolbox provided here : https://github.com/IoSR-Surrey/MatlabToolbox, but it does not seem to be able to do this. Does anyone has any suggestion?
Thanks in advance.
0 comentarios
Respuesta aceptada
Adam Danz
el 7 de Mayo de 2021
Editada: Adam Danz
el 7 de Mayo de 2021
Here are examples using boxchart (requires Matlab r2020a) and boxplot with slightly different outcomes.
rng('default')
x = -pearsrnd(0,1,1,4,1000,8);
figure()
axes()
hold on
% Loop through each box in order to control color
for i = 1:size(x,2)
boxchart(repmat(i,size(x,1),1),x(:,i),'Orientation','horizontal');
end
legend(num2cell(char(double(64+(1:size(x,2))))),'Location','BestOutside')
box on
Demo using boxplot
boxplot() also has an orientation option but it does not fill the color of the boxplots (unless 'compact' style is used) and does not have a legend option.
rng('default')
x = -pearsrnd(0,1,1,4,1000,8);
groups = (1:size(x,2)) .* ones(size(x));
figure()
group = (1:size(x,2)).*ones(size(x,1),1);
boxplot(x(:),group(:),'Orientation','horizontal',...
'ColorGroup',lines(size(x,2)),'Symbol','o','OutlierSize',4);
3 comentarios
Adam Danz
el 7 de Mayo de 2021
I added another example to my answer using boxplot which existed long before boxchart but it doesn't have options to fill the boxplots nor does it easily support a legend.
Adam Danz
el 7 de Mayo de 2021
Editada: Adam Danz
el 2 de Dic. de 2021
Old version of example1
I'm storing this here for my own reference. It demonstrates some bugs in r2021a when using grouped data and horizontal orientation. Use the code from my answer above and ignore this.
Note: this has been fixed in R2021b.
Problem:
- XRuler and YRuler and not switched
- XRuler is categorical desipte using numeric inputs.
- XAxis limit isn't adjusted (axis tight fixes it)
rng('default')
x = -pearsrnd(0,1,1,4,1000,8);
groups = (1:size(x,2)) .* ones(size(x));
figure()
boxchart(x(:),'GroupByColor',groups(:),'Orientation','horizontal');
% axis tight % This fixes the xlim problem
legend(num2cell(char(double(64+(1:size(x,2))))),'Location','BestOutside')
Más respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!