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

New to MATLAB?

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

Asked by Doug Hull

Doug Hull (view profile)

on 11 Jan 2011
Latest activity Commented on by Hillaryfor2016

Hillaryfor2016 (view profile)

on 10 Feb 2015

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 (view profile)


2 Answers

Answer by Doug Hull

Doug Hull (view profile)

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 (view profile)

on 24 May 2012

This example just saved my life.

Mark Cejas

Mark Cejas (view profile)

on 18 Mar 2013

This is a great example! thanks


Julia (view profile)

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

Doug Hull (view profile)

Answer by Tom Lane

Tom Lane (view profile)

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

Tom Lane (view profile)

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.


Algis (view profile)

on 28 Dec 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 (view profile)

on 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

Tom Lane

Tom Lane (view profile)

Contact us