Making a box plot

조회 수: 10 (최근 30일)
Jay Stewart
Jay Stewart 2018년 8월 17일
편집: Adam Danz 2018년 8월 20일
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일
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
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일
I don't understand what the problem could be. I re ran the code again. Same issue

카테고리

Help CenterFile Exchange에서 Axis Labels에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by