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

30 views (last 30 days)
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.

Accepted Answer

Doug Hull
Doug Hull on 12 Jan 2011
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 Comments
Denise Napolitano
Denise Napolitano on 5 Jun 2018
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 on 13 Jun 2018
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.

Sign in to comment.

More Answers (2)

Tom Lane
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');
  10 Comments

Sign in to comment.


Mohammad Abu Zafer Siddik
Mohammad Abu Zafer Siddik on 18 Feb 2019
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 Comment
Ellyn Gray
Ellyn Gray on 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

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!