How do I display different boxplot groups on the same figure in MATLAB?
古いコメントを表示
I would like to generate boxplots on the same figure for two different groups, observed and simulated temperatures for January to December with the x-axis being months. The two different groups need to be represented in different colors. The spacing between the boxplots also need to be user-definable.
採用された回答
その他の回答 (2 件)
Tom Lane
2012 年 5 月 25 日
The boxplot function has more options than you can shake a stick at. Try this:
data = rand(20,24)
month = repmat({'jan' 'feb' 'mar' 'apr' 'may' 'jun' 'jul' 'aug' 'sep' 'oct' 'nov' 'dec'},1,2);
simobs = [repmat({'sim'},1,12),repmat({'obs'},1,12)];
boxplot(data,{month,simobs},'colors',repmat('rb',1,12),'factorgap',[5 2],'labelverbosity','minor');
10 件のコメント
Ville-Pekka Seppä
2013 年 1 月 9 日
Thanks, this is quite helpful. In my case however the data consists of 9 categories each divided into 4 subcategories which do not contain equal amount of values.
I would like to plot this divided into nice groups (each having 4 boxes) and be able to choose the order of the groups. Especially choosing the order of the groups is giving me trouble as the Matlab help is rather vague on that for multiple grouping variables.
Any help appreciated!
Tom Lane
2013 年 1 月 9 日
Consider making ordinal variables to specify the order. For example:
load carsmall
boxplot(MPG,{Origin Model_Year}) % default ordering
org = ordinal(Origin,[],{'France' 'Germany' 'Sweden' 'Italy' 'Japan' 'USA'});
boxplot(MPG,{org Model_Year}) % European countries grouped together
yr = ordinal(Model_Year,[],[82 76 70]);
boxplot(MPG,{org yr}) % years running backward
Ville-Pekka Seppä
2013 年 1 月 10 日
Ordinal variables were the key to this. Thanks!
Berk
2013 年 5 月 13 日
Tom, is it possible to apply your code in your initial reply to situations where the groups do not have the same number of observations? For example, in your example each Month/Sim or Month/Obs couple has 20 observations. Would it be possible to make this more flexible so that Obs, for example, has more observations than Sim?
Tom Lane
2013 年 5 月 15 日
The example that begins "load carsmall" has different numbers of observations for each value or Origin. So arranging your data in a vector with a corresponding grouping value is one way to do what you want.
In the example with "simobs" I set up a matrix, so it naturally has the same number of values in each column. It would be possible to pad each column with NaN values if there were not an equal number of numeric values for each column.
Julia
2014 年 7 月 29 日
I didn't get how to set up my data for this code. I tried having 24 columns, with the first 12 being the first set and the second 12 being the second set. The data were lumped together instead of being displayed side-by-side.
Tom Lane
2014 年 7 月 31 日
My May 2012 example shows how to do this. You'll need to explain what you want, what you did, and what you got if you want more advice.
Algis
2014 年 12 月 28 日
This implementation seems to be wrong, because for each boxplot it takes not 20 elments, but somehow 40. Can you suggest on how to fix it?
Hillaryfor2016
2015 年 2 月 10 日
I couldn't get this method to work for my code. I instead used a function 'boxplot2' and it worked a dream. See http://uk.mathworks.com/matlabcentral/answers/176766-multiple-boxplots-for-a-single-plot
Mohammad Naser
2019 年 12 月 13 日
Thanks
Mohammad Abu Zafer Siddik
2019 年 2 月 18 日
0 投票
Hi Tom Lane,
Thank you for your code, i usee your code in the folowing way.
boxplot(num,'colors',repmat('gbrm',1,13),'PlotStyle','compact','symbol','+');
However, i need to give RGB such as "[25 150 81]./255" for green and so on for other colors. Because, the default green is not clear when we print black and white. Would you please tell me a way that how can i use RGB for different darker colors?
Thanks
Mohammad
1 件のコメント
Ellyn Gray
2019 年 2 月 20 日
This is the most useful thing I've found for finding colors. It can be a bit of a guessing game testing colors without it.
https://www.mathworks.com/matlabcentral/fileexchange/24497-rgb-triple-of-color-name-version-2
カテゴリ
ヘルプ センター および File Exchange で Data Distribution Plots についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!