boxplot with vectors of different lengths

302 ビュー (過去 30 日間)
Vahid
Vahid 2013 年 2 月 1 日
編集済み: Joris Bockhofer 2023 年 7 月 5 日
Hi MATLAB folks,
I am wondering how I can boxplot two column matrices with different lengths, e.g.
c_1=rand(1,20);
c_2=rand(1,100);
how I can do
boxplot(C);
where:
C=cell(1,2);
C{1}=c_1(:);
C{2}=c_2(:);
Is there any solution to that?
Many thanks in advance, -V

採用された回答

Shashank Prasanna
Shashank Prasanna 2013 年 2 月 1 日
BOXPLOT works with grouping variables, so you can manually append all of your data together and then create a grouping variable that lets boxplot know which belongs to first and which for second. Take a look at the example below:
>> c_1=rand(1,20);
>> c_2=rand(1,100);
>> C = [c_1 c_2];
>> grp = [zeros(1,20),ones(1,100)];
>> boxplot(C,grp)
  6 件のコメント
matteo bottoni
matteo bottoni 2020 年 12 月 4 日
Hi, how can I set a name for each x-coordinate and not simply a number?
Adam Danz
Adam Danz 2020 年 12 月 9 日
編集済み: Adam Danz 2020 年 12 月 9 日
Set the labels property of a boxplot.
Alternatively, set xtick and xticklabel which are both axis properties.

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

その他の回答 (2 件)

Matt Raum
Matt Raum 2017 年 3 月 23 日
I ran into the same issue -- here's a quick re-wrapping of boxplot that can be called on a cell array containing vectors of variable size:
col=@(x)reshape(x,numel(x),1);
boxplot2=@(C,varargin)boxplot(cell2mat(cellfun(col,col(C),'uni',0)),cell2mat(arrayfun(@(I)I*ones(numel(C{I}),1),col(1:numel(C)),'uni',0)),varargin{:});
boxplot2 automatically generates the necessary grouping array. All you need to pass to it is a cell array of the vectors you want box plotted.
boxplot2({randn(1,100),10+2*randn(200,1)});
  4 件のコメント
Aihong CUI
Aihong CUI 2022 年 11 月 29 日
very useful, thank you very much
Joris Bockhofer
Joris Bockhofer 2023 年 7 月 5 日
編集済み: Joris Bockhofer 2023 年 7 月 5 日
You are a hero. The hero MatLab doesnt deserve....
I want to add that this will be a problem when trying to identify boxes belonging to a certain group.
To "fix" this create an array of strings that map the number of the, now numbered boxes to a legend label and add a legend in a hacky way :)
legendStrArray = string();
for i = 1:numBoxes
legendStrArray(1,end+1) = string(i) + " = " + yourListOfGroupLabels(i); %
end
legendStrArray = legendStrArray(1,2:end);
hLegend = legend(findall(gca,'Tag','Box'), legendStrArray); % finds a figure with the tag box which should be your boxplot

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


Joey Porter
Joey Porter 2020 年 4 月 21 日
This is my first feedback so please forgive if I've misunderstood but I solved this issue with a simpler explanation (at least for me).
boxplot plots each columns as a separate variable (box) as long as the lengths of each column are the same.
I simply created a NaN matrix with number of rows equal to the length of my longest variable, then populated with my variable data. The NaNs are not plotted on the box plot but allows variables of unequal length to be plotted. Also to add more variables, simply increase the number of columns in your NaN array.
I hope this helps anyone returning to this thread :)
  3 件のコメント
Joey Porter
Joey Porter 2020 年 4 月 21 日
Thanks Adam.
I didn't know there was a function for padding. I'll use this next time!
Camilo Cárdenas
Camilo Cárdenas 2022 年 4 月 29 日
Gracias! excelente dato!

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

Community Treasure Hunt

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

Start Hunting!

Translated by