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.

 採用された回答

Doug Hull
Doug Hull 2011 年 1 月 12 日

7 投票

There is no direct way of displaying boxplots for two different groups (in this example, observed and simulated temperatures) on the same figure.
As a workaround, boxplots can be generated at defined positions for one group first. HOLD ON allows the boxplots for the second group to display on the same figure.
In the following example, boxplots of the second group are 0.2 units away (user defined) from their corresponding blue boxplots.
% {Display boxplots for two different groups,
% observed and simulated temperatures from January to December on the same figure}%
close all
clc
data = textread('bp.txt'); % Read user’s data file
%{Assign the observed temperature to be Month_O and the simulated
% temperature to be Month_S}%
Jan_O = data(:, 1);
Jan_S = data(:, 2);
Feb_O = data(:, 3);
Feb_S = data(:, 4);
Mar_O = data(:, 5);
Mar_S = data(:, 6);
Apr_O = data(:, 7);
Apr_S = data(:, 8);
May_O = data(:, 9);
May_S = data(:,10);
Jun_O = data(:,11);
Jun_S = data(:,12);
Jul_O = data(:,13);
Jul_S = data(:,14);
Aug_O = data(:,15);
Aug_S = data(:,16);
Sep_O = data(:,17);
Sep_S = data(:,18);
Oct_O = data(:,19);
Oct_S = data(:,20);
Nov_O = data(:,21);
Nov_S = data(:,22);
Dec_O = data(:,23);
Dec_S = data(:,24);
f=figure;
% Boxplot for the observed temperature from January to December
Temp_O = [Jan_O, Feb_O, Mar_O, Apr_O, May_O, Jun_O, Jul_O, Aug_O, Sep_O, Oct_O, Nov_O, Dec_O];
position_O = 1:1:12;
% Define position for 12 Month_O boxplots
box_O = boxplot(Temp_O,'colors','b','positions',position_O,'width',0.18);
set(gca,'XTickLabel',{' '}) % Erase xlabels
hold on % Keep the Month_O boxplots on figure overlap the Month_S boxplots
% Boxplot for the simulated temperature from January to December
Temp_S = [Jan_S, Feb_S, Mar_S, Apr_S, May_S, Jun_S, Jul_S, Aug_S, Sep_S, Oct_S, Nov_S, Dec_S];
position_S = 1.3:1:12.3; % Define position for 12 Month_S boxplots
box_S = boxplot(Temp_S,'colors','r','positions',position_S,'width',0.18);
hold off % Insert texts and labels
ylabel('Temperature')
text('Position',[1.1,0],'String','January')
text('Position',[2.1,0],'String','February')
text('Position',[3.1,0],'String','March')
text('Position',[4.1,0],'String','April')
text('Position',[5.1,0],'String','May')
text('Position',[6.1,0],'String','June')
text('Position',[7.1,0],'String','July')
text('Position',[8.1,0],'String','August')
text('Position',[9.1,0],'String','September')
text('Position',[10.1,0],'String','October')
text('Position',[11.1,0],'String','November')
text('Position',[12.1,0],'String','December')
set(gca,'XTickLabel',{''}); % To hide outliers
out_O = box_O(end,~isnan(box_O(end,:)));
delete(out_O)
out_S = box_S(end,~isnan(box_S(end,:)));
delete(out_S)

7 件のコメント

Martha
Martha 2012 年 5 月 24 日
This example just saved my life.
Mark Cejas
Mark Cejas 2013 年 3 月 18 日
This is a great example! thanks
Julia
Julia 2014 年 7 月 29 日
I copied this code and substituted my data, but the x labels aren't showing up. Any advice? Thanks.
Kevin Heimbach
Kevin Heimbach 2016 年 8 月 27 日
編集済み: Kevin Heimbach 2016 年 8 月 27 日
amazing example, needed this exactly
Sayantan Sahu
Sayantan Sahu 2017 年 8 月 11 日
What should I do to get the 5th and 95th percentile ?
Excellent code, thank you! Do you have any advice on getting the object handles to modify box properties? I usually use a = get(get(gca,'children'),'children'); but 'a' is interpreted as a 2x1 cell array (21x1 lines, since there are 3 sets of stacked data):
a =
2×1 cell array
[21×1 Line]
[21×1 Line]
Ellyn Gray
Ellyn Gray 2018 年 6 月 13 日
Thank you so much!!! I spent hours trying to do this with boxplot and boxplot2 and your solution worked in 10 mins. Exactly what I wanted and your example was super easy to follow.

サインインしてコメントする。

その他の回答 (2 件)

Tom Lane
Tom Lane 2012 年 5 月 25 日

7 投票

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ä
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
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ä
Ville-Pekka Seppä 2013 年 1 月 10 日
Ordinal variables were the key to this. Thanks!
Berk
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
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
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
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
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
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
Mohammad Naser 2019 年 12 月 13 日
Thanks

サインインしてコメントする。

Mohammad Abu Zafer Siddik
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
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 ExchangeData Distribution Plots についてさらに検索

製品

質問済み:

2011 年 1 月 11 日

コメント済み:

2019 年 12 月 13 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by