2015-09-16 88 views
0

我使用Matlab繪製了一個簡單的向量y(1xN)箱形圖。我使用多個分組變量:X1,X2,X3使用Matlab在boxplot上繪製散點圖

X1(1×N個)表示長度(0.5,1,2或3)

X2(1×N個)表示表壓(26或30)

X3 (1xN單元陣列)表示供應商的名稱。

close all; clc; 

N = 1000; 


% measurements values: they represent some kind of an 
% electrical characteristic of a cable. 
y = randn(N,1); 

% each cable being measured can be of length 1m, 2m, or 3m: 
x1 = randi(3,N,1); 

% each cable being measured have a gauge of 1awg or 2awg: 
x2 = randi(2,N,1); 

% each cable can be produced by a different vendor. for instance: 'SONY' or 
% 'YAMAHA' 

x3 = cell(N,1); 

for ii = 1:N 
    if mod(ii,3) == 0 
     x3{ii} = 'SONY'; 
    else 
     x3{ii} = 'YAMAHA'; 
    end 
end 

figure(1) 
boxplot(y,{x1,x2,x3}); 

我想繪製了這箱線圖散點圖以表明創建箱線圖Y的相關數值,但我無法找到組值作爲箱線圖功能做了功能。

我發現的最接近的東西是function,但它只接受一個分組變量。

有幫助嗎?

回答

0

boxlot的盒子由IQR決定。盒子和異常值之間的數據是從上限和下限四分位數的1.5 * IQR範圍內的所有數據。您可以手動過濾數據。

例如...

% data generation 
data=randn(100,3); 

%% 
datas=sort(data); 
datainbox=datas(ceil(end/4)+1:floor(end*3/4),:); 

[n1 n2]=size(datainbox); 

figure(1);clf 
boxplot(data); hold on 
plot(ones(n1,1)*[1 2 3],datainbox,'k.') 

%% 
% All datapoints coincide now horizontally. Consider adding a little random 
% horizontal play to make them not coincide: 

figure(2);clf 
boxplot(data); hold on 
plot(ones(n1,1)*[1 2 3]+.4*(rand(n1,n2)-.5),datainbox,'k.') 

%% 
% If you want to add all data between boxes and outliers too, do something like: 

dataoutbox=datas([1:ceil(end/4) floor(end*3/4)+1:end],:); 
n3=size(dataoutbox,1); 
% calculate quartiles 
dataq=quantile(data,[.25 .5 .75]); 
% calculate range between box and outliers = between 1.5*IQR from quartiles 
dataiqr=iqr(data); 
datar=[dataq(1,:)-dataiqr*1.5;dataq(3,:)+dataiqr*1.5]; 
dataoutbox(dataoutbox<ones(n3,1)*datar(1,:)|dataoutbox>ones(n3,1)*datar(2,:))=nan; 

figure(3);clf 
boxplot(data); hold on 
plot(ones(n1,1)*[1 2 3]+.4*(rand(n1,n2)-.5),datainbox,'k.') 
plot(ones(n3,1)*[1 2 3]+.4*(rand(n3,n2)-.5),dataoutbox,'.','color',[1 1 1]*.5) 
+0

感謝您的努力,但你可以從我剛纔說的例子代碼中看到的,我用很多組向量,它們中的一些字符串。 – TheUpvoter

0

發現了一個簡單的解決方案:

我編輯的「箱線圖」功能的簽名,以便將除了「H」返回「groupIndexByPoint」:

函數[h時,groupIndexByPoint] =箱線圖(varargin)

groupIndexByPoint是內部雜物由'boxplot'使用。

,現在只需添加4行原代碼:

N = 1000; 

% measurements values: they represent some kind of an 
% electrical characteristic of a cable. 
y = randn(N,1); 

% each cable being measured can be of length 1m, 2m, or 3m: 
x1 = randi(3,N,1); 

% each cable being measured have a gauge of 1awg or 2awg: 
x2 = randi(2,N,1); 

% each cable can be produced by a different vendor. for instance: 'SONY' or 
% 'YAMAHA' 

x3 = cell(N,1); 

for ii = 1:N 
    if mod(ii,3) == 0 
     x3{ii} = 'SONY'; 
    else 
     x3{ii} = 'YAMAHA'; 
    end 
end 

figure(1); 
hold on; 
[h,groups] = boxplot(y,{x1,x2,x3}); 
scattering_factor = 0.3; 
scaterring_vector = (rand(N,1)-0.5)*scattering_factor; 
groups_scattered = groups + scaterring_vector; 
plot(groups_scattered,y,'.g');