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.
採用された回答
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 日
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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
