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!