MATLAB Answers

Create a grouped boxplot

10 ビュー (過去 30 日間)
NA
NA 2021 年 7 月 7 日
編集済み: Adam Danz 2021 年 8 月 31 日
I have 3 groups of data A, B, and C.
A = rand(100,8);
B = rand(100,8)*2;
C = rand(100,8)*4;
I want to plot Box chart of grpup A,B and C.
The internal label of each group is {'a' 'b' 'c' 'd' 'e' 'f' 'g' 'h'}.
I found boxplotgroup(), and used below code
data = {A,B,C};
boxplotGroup(data, 'PrimaryLabels', {'a' 'b' 'c' 'd' 'e' 'f' 'g' 'h'}, ...
'SecondaryLabels',{'A', 'B' 'C'}, 'InterGroupSpace', 3)
But I got this error.
Error using assert (line 5)
The number of primary labels must equal either the number of bars per group
(3) or the number of total bars (24).
Error in boxplotGroup (line 155)
assert(ismember(numel(p.Results.primarylabels),[nMembers,
nMembers*nGroups]), ...
Error in (line 13)
boxplotGroup(data, 'PrimaryLabels', {'a' 'b' 'c' 'd' 'e' 'f' 'g' 'h'}, ...

採用された回答

Christopher McCausland
Christopher McCausland 2021 年 7 月 7 日
編集済み: Christopher McCausland 2021 年 7 月 7 日
Hi NA,
From the documentation here (See section boxplotGroup(__,'primaryLabels',c) ) the number of primary lables should match the number of boxplotGroups. i.e. you appear to be plotting three box plots (A,B,C) but trying to give these box plots eight names ('a' 'b' 'c' 'd' 'e' 'f' 'g' 'h').
I beleive that you are trying to show something like the last example in boxplotGroup(__,'secondaryLabels',s) (shown below) in which case swap your primary and secondary labels.
Let me know if that helps,
Christopher
  2 件のコメント
Adam Danz
Adam Danz 2021 年 8 月 31 日
@Christopher McCausland thanks for providing support.
@NA I know the boxplotGroup inputs are not intuitive. I may try to improve that in the future while maintaining backward compatibility.
If A B and C are all the same size, here's the solution. If they aren't the same size, you need to pad the smaller matrices with NaN values to make the equal.
A = rand(100,8);
B = rand(100,8)*2;
C = rand(100,8)*4;
D = squeeze(mat2cell(permute(cat(3,A,B,C),[1,3,2]),size(A,1),3,ones(1,size(A,2))));
% Number of matricies ^
clf
boxplotGroup(D','PrimaryLabels', {'a' 'b' 'c' 'd' 'e' 'f' 'g' 'h'}, ...
'SecondaryLabels',{'A', 'B' 'C'}, 'InterGroupSpace', 3)

サインインしてコメントする。

その他の回答 (0 件)

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by