Drawing fancy box plots

78 Ansichten (letzte 30 Tage)
omid zandi
omid zandi am 7 Mai 2021
Bearbeitet: Adam Danz am 2 Dez. 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.

Akzeptierte Antwort

Adam Danz
Adam Danz am 7 Mai 2021
Bearbeitet: Adam Danz am 7 Mai 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 Kommentare
Adam Danz
Adam Danz am 7 Mai 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
Adam Danz am 7 Mai 2021
Bearbeitet: Adam Danz am 2 Dez. 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:
  1. XRuler and YRuler and not switched
  2. XRuler is categorical desipte using numeric inputs.
  3. 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')

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by