How do I display different boxplot groups on the same figure in MATLAB?
Ältere Kommentare anzeigen
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.
Akzeptierte Antwort
Weitere Antworten (2)
Tom Lane
am 25 Mai 2012
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 Kommentare
Ville-Pekka Seppä
am 9 Jan. 2013
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
am 9 Jan. 2013
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ä
am 10 Jan. 2013
Ordinal variables were the key to this. Thanks!
Berk
am 13 Mai 2013
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
am 15 Mai 2013
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
am 29 Jul. 2014
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
am 31 Jul. 2014
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
am 28 Dez. 2014
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
am 10 Feb. 2015
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
am 13 Dez. 2019
Thanks
Mohammad Abu Zafer Siddik
am 18 Feb. 2019
0 Stimmen
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 Kommentar
Ellyn Gray
am 20 Feb. 2019
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
Kategorien
Mehr zu Data Distribution Plots finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!