I have a 10x51 matrix and was trying to make 2 box plots with the first 5 rows showing column 1 data and the next 5 rows of showing column 1 data as well. For example if the matrix had 10 numbers with the first 5 being 1 and the next 5 being 2. How do I make a figure that creates two box plots and compares them against each other. Need to do this for all 51 columns of data.

 採用された回答

Adam Danz
Adam Danz 2018 年 8 月 17 日
編集済み: Adam Danz 2018 年 8 月 17 日

0 投票

I'm not sure how you'd like to arrange the 51 plots.
Here's a template you can follow. It plots the first 6 columns data but you could change that by changing 'n' to whatever you'd like. Just keep in mind that the figure will become cluttered very quickly.
I first create fake data then I loop through the first 'n' columns and compare the first and second halves of the column as you described.
%fake data
m = rand([10,51]) .* randi(50, 1,51);
figure
n = 6;
for i = 1:n
subplot(1,n,i)
tempData = reshape(m(:,i), [], 2);
boxplot(tempData)
xlabel('col half')
title(sprintf('Col %d', i));
end
Here's the version that produces a new figure every 'n' columns so that all of your data are plotted.
% How many subplots per figure?
n = 10;
% That will produced this many figures
nFigs = ceil(size(m,2) / n);
% Loop through figures
c = 0; %column counter
for j = 1:nFigs
figure
for i = 1:n
c = c+1;
if c > size(m,2)
continue
end
subplot(1,n,i)
tempData = reshape(m(:,c), [], 2);
boxplot(tempData)
xlabel('col half')
title(sprintf('Col %d', c));
end
end

13 件のコメント

Jay Stewart
Jay Stewart 2018 年 8 月 17 日
I copied your code and only got 1 box plot to show up. I changed the n from 6 to 10, hoping to get 10 plots but that didn't work. I was also wondering as to how to create another figure with the next 10 columns as I don't want to reuse any data from the previous columns
Adam Danz
Adam Danz 2018 年 8 月 17 日
編集済み: Adam Danz 2018 年 8 月 17 日
You must have copied it wrong or maybe you didn't execute the entire code. I'm assuming you copied and executed my fake data as well.
I just copied the whole thing and ran it again and it works (produced the same image but with different random fake data).
I updated my answer to show you how to produce a new figure every 10 columns. Tested, works.
If it doesn't work with your data we need to find out how your data differ from my fake data.
Jay Stewart
Jay Stewart 2018 年 8 月 17 日
Okay, it works perfectly. I have an additional question because instead of sorting the column by 5 and 5, I need to label certain rows as x and other rows as y and then plot them against each other. This would organize them by class, essentially. How would I do so?
Adam Danz
Adam Danz 2018 年 8 月 17 日
編集済み: Adam Danz 2018 年 8 月 17 日
I'm glad it worked! If you accept the answer (by pressing accept) future viewers will know that this solution worked.
What have you tried so far to implement your new idea?
I'll give you a hint
help boxplot
Read about "boxplot(X,G)"
Jay Stewart
Jay Stewart 2018 年 8 月 20 日
I made a matrix created with 1s and 0s to distinguish between the data to make each box plot. I tried to add it to the line of code you made simply with boxplot(tempdata,g), but that did not render all the box plots. only about half. what should I try next?
Adam Danz
Adam Danz 2018 年 8 月 20 日
In this example, I make two groups of data (groups 1 and 2) and I assign each row of the matrix 'm' to a group.
groups = [1 1 2 2 1 1 2 2 1 2]';
for i = 1:n
subplot(1,n,i)
boxplot(m(:,i), groups)
xlabel('col half')
title(sprintf('Col %d', i));
end
Jay Stewart
Jay Stewart 2018 年 8 月 20 日
Okay, that fixed the problem of not having all the box plots showing up and classifying them by their respective groups. However, now the same box plot is rendered for all 51 columns
Jay Stewart
Jay Stewart 2018 年 8 月 20 日
This is the current code
if true
% code
end
n=2;
nFigs = ceil(size(m,2) / n);
c=0;
for j = 1:nFigs
figure
for i = 1:n
c = c+1;
if c > size(m,2)
continue
end
subplot(1,n,i)
boxplot(m(:,1),groups)
xlabel('col half')
title(sprintf('Col %d', c));
end
end
Adam Danz
Adam Danz 2018 年 8 月 20 日
編集済み: Adam Danz 2018 年 8 月 20 日
That's because you're only requesting the first column of M to be plotted on each figure.
boxplot(m(:,1),groups); % m(:,1) is the first col of m
replace 1 with c to iterate through each col of m.
Jay Stewart
Jay Stewart 2018 年 8 月 20 日
But then I got the error: "Index in position 2 is invalid. Array indices must be positive integers or logical values
Jay Stewart
Jay Stewart 2018 年 8 月 20 日
because c is just a 1x1 double matrix with its lone value being 52
Adam Danz
Adam Danz 2018 年 8 月 20 日
編集済み: Adam Danz 2018 年 8 月 20 日
Then something else is wrong in your code. c should be a double, 1x1 matrix and as your code indicates, iterate through 1 to 51.
If I copy your code from 4 comments up, replace the '1' in the boxplot() call with c, and I add the grouping variable
groups = [1 1 2 2 1 1 2 2 1 2]';
it works.
Adam Danz
Adam Danz 2018 年 8 月 20 日
編集済み: Adam Danz 2018 年 8 月 20 日
Here's your code with the two changes I summarized above. Copy this and run this on your matrix 'm'. If you have an error, please copy the full error message into the comments section and copy the line of code that produced the error.
groups = [1 1 2 2 1 1 2 2 1 2]'; % Add this
n=2;
nFigs = ceil(size(m,2) / n);
c=0;
for j = 1:nFigs
figure
for i = 1:n
c = c+1;
if c > size(m,2)
continue
end
subplot(1,n,i)
boxplot(m(:,c),groups) %Changed 1 to c
xlabel('col half')
title(sprintf('Col %d', c));
end
end

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

その他の回答 (1 件)

Jay Stewart
Jay Stewart 2018 年 8 月 20 日

0 投票

I don't understand what the problem could be. I re ran the code again. Same issue

タグ

質問済み:

2018 年 8 月 17 日

編集済み:

2018 年 8 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by