Making a box plot
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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.
채택된 답변
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
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
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
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?
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
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
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
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
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
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
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
2018년 8월 20일
because c is just a 1x1 double matrix with its lone value being 52
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.
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
2018년 8월 20일
0 개 추천
I don't understand what the problem could be. I re ran the code again. Same issue
카테고리
도움말 센터 및 File Exchange에서 Exploration and Visualization에 대해 자세히 알아보기
태그
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
