Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

How do I display different boxplot groups on the same figure in MATLAB?

Asked by Doug Hull on 11 Jan 2011
Latest activity Commented on by Tom Lane on 31 Jul 2014

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


2 Answers

Answer by Doug Hull on 12 Jan 2011
Accepted answer

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  
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);   
% 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 
set(gca,'XTickLabel',{''});   % To hide outliers 
out_O = box_O(end,~isnan(box_O(end,:)));  
out_S = box_S(end,~isnan(box_S(end,:)));  


Martha on 24 May 2012

This example just saved my life.

Mark Cejas on 18 Mar 2013

This is a great example! thanks

Julia on 29 Jul 2014

I copied this code and substituted my data, but the x labels aren't showing up. Any advice? Thanks.

Doug Hull
Answer by Tom Lane on 25 May 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');


Tom Lane on 15 May 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 on 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 on 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.

Tom Lane

Contact us