How do I display different boxplot groups on the same figure in MATLAB 7.9 (R2009b)?

조회 수: 1 (최근 30일)
Shigeru Sasao
Shigeru Sasao 2011년 1월 13일
댓글: Thomas Vargas 2014년 2월 19일
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.

답변 (2개)

Siddharth
Siddharth 2011년 1월 18일
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. Example code:
% {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)

Tom Lane
Tom Lane 2011년 1월 23일
Start with this, modify as appropriate:
month = {'Jan' 'Feb' 'Mar' 'Apr'}';
obsim = {'Obs' 'Sim'}';
n = 400;
boxplot(randn(n,1),{month(randi(4,n,1)),obsim(randi(2,n,1))},...
'factorsep',1,'factorgap',10)

카테고리

Help CenterFile Exchange에서 Exploration and Visualization에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by