Multiple boxplots for a single plot

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.

 Risposta accettata

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 Commenti

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.
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.
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 il 9 Feb 2015
Modificato: Hillaryfor2016 il 10 Feb 2015
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?
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
Many Many thanks!!!!
I love your work ! Thanks a lot!

Accedi per commentare.

Più risposte (1)

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by