I have 8 matrices of nX5 dimensions that I wish to plot as boxplots on the same figure. Ideally the figure will show 8 boxplots for each of the 5 columns (i.e. 40 individual box plots). Ive struggled with trying to get the hold on functionality to work, likewise using a positional variable for each plot. Likewise Im not sure how to 'group' the individual columns from the different matrices.
PS Have attached the variable 'xvar.m' which I wish to boxplot.

 採用された回答

Kelly Kearney
Kelly Kearney 2015 年 2 月 9 日

7 投票

I wrote boxplot2 to handle data like this; it plots boxplots similar to grouped bar plots:
% Some data, 8 x 1 cell array with 100 x 5 arrays
x = cell(8,1);
for ix = 1:8
x{ix} = randn(100,5);
end
% Plot
boxplot2(permute(cat(3, x{:}), [2 3 1]), 1:5)

7 件のコメント

Hillaryfor2016
Hillaryfor2016 2015 年 2 月 9 日
編集済み: Hillaryfor2016 2015 年 2 月 9 日
Hi Kelly,
Thank you so much for the reply. However Im having a slight bit of bother with your code and your continued assistance would be very much appreciated! I set the cell array values to my arrays that I am using
x = cell(8,1);
x{1,1}=Fr; %11x5
x{2,1}=Kh; %1x5
x{3,1}=Me; %43x5
x{4,1}=Me2; %14x5
x{5,1}=Mi; %32x5
x{6,1}=La; %2x5
x{7,1}=Bo; %1x5
x{8,1}=Mu; %7x5
end
Using boxplot2(permute(cat(3, x{:}), [2 3 1]), 1:5) results in the data being unsuccessfully concatenated as I would have thought given the different sizes of matrices.
Cheers and thanks once again for the reply. Ive spent my afternoon slowly questioning if I really cannot make do with a scatter plot!
PS Have attached 'xvar.m' as per star strider's request.
Star Strider
Star Strider 2015 年 2 月 9 日
It may help if you save your ‘x’ variable as a .mat file and attach it (use the ‘paperclip’ (or ‘staple’) icon to upload it here, preferably to your original Question or in a Comment to it.
Kelly Kearney
Kelly Kearney 2015 年 2 月 9 日
The boxplot command ignores NaNs in its calculations, so for uneven data I usually just pad the array out with NaNs. You can do that manually or with this little function:
%%%%% Save as catuneven.m %%%%%
function b = catuneven(dim, padval, varargin);
ndim = max(cellfun(@ndims, varargin));
ndim = max(ndim, dim);
for ii = 1:ndim
sz(:,ii) = cellfun(@(x) size(x, ii), varargin);
end
maxsz = max(sz, [], 1);
nv = length(varargin);
val = cell(size(varargin));
for ii = 1:nv
sztmp = maxsz;
sztmp(dim) = sz(ii,dim);
idx = cell(ndim,1);
[idx{:}] = ind2sub(sz(ii,:), 1:numel(varargin{ii}));
idxnew = sub2ind(sztmp, idx{:});
val{ii} = ones(sztmp) * padval;
val{ii}(idxnew) = varargin{ii};
end
b = cat(dim, val{:});
%%%%%
Then same commands as above:
load('xvar.mat');
x = catuneven(3, NaN, x{:});
boxplot2(permute(x, [3 2 1]), 1:8);
Hillaryfor2016
Hillaryfor2016 2015 年 2 月 9 日
編集済み: Hillaryfor2016 2015 年 2 月 10 日
Yep, I figured this out (manual fudging) but thanks for the padding function, very helpful for future!!
Any tips on how to avoid manually colouring each individual group in the editor?
Kelly Kearney
Kelly Kearney 2015 年 2 月 9 日
The output variable returns all the handles to the objects. You can change color, linestyle, marker type, etc. using those (and I often concatenate things if I want to change a lot of them):
h = boxplot2(permute(x, [3 2 1]), 1:8);
cmap = jet(8);
tmp = struct2cell(h);
tmp = cat(3,tmp{:});
for ii = 1:8
set(tmp(:,ii,:), 'color', cmap(ii,:));
set(h.out(:,ii), 'markeredgecolor', cmap(ii,:));
end
Hillaryfor2016
Hillaryfor2016 2015 年 2 月 10 日
Many Many thanks!!!!
檮杌
檮杌 2016 年 12 月 6 日
I love your work ! Thanks a lot!

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

その他の回答 (1 件)

Haifeng Lu
Haifeng Lu 2016 年 3 月 19 日

0 投票

Thanks a lot! It really helps me!

製品

Community Treasure Hunt

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

Start Hunting!

Translated by