2017-07-03 72 views
2

我有一個xyz球中點的三維散點圖。我想知道是否有方法根據數據的密度對散點圖進行色彩映射/色調。基本上,數據點密度最密集的散點圖的部分將是暗紅色,半稠密聚類的數據點將是中等紅色,疏散聚類的數據點將是淺紅色。根據點密度計算三維散點圖的顏色代碼點

這是我想到的方式,但(希望)可能會有一個更簡單的函數或命令來做到這一點。

設置在散射數據點必須由包圍的閾值:

[> = 10個其它點半徑1的範圍內被着色深紅,

[5-9半徑爲1的球體內的其他點爲中等紅色,並且

[0-4在半徑爲1的球體內被染成淺紅色。

當然,我希望有一個更簡單的方法來做到這一點,在顏色映射中涉及超過3種顏色,所以如果任何人有任何想法如何編碼,我會很感激幫助!非常感謝。

這裏是我的數組的一個片段:

184 115 3915 
185 115 3916 
185 115 1205 
186 115 4094 
187 115 2237 
192 115 1519 
193 115 1327 
201 115 1170 
240 115 2946 
241 115 1332 
54 116 1244 
58 116 3650 
59 116 3984 
60 116 1631 
61 116 1198 
61 116 1194 
62 116 1189 
65 116 1185 
186 116 3669 
188 116 3986 
189 116 2027 
197 116 1200 
201 116 1254 
226 116 3752 
227 116 1457 
242 116 1405 
54 117 1191 
54 117 1305 
56 117 1177 
58 117 1169 
61 117 1367 
62 117 1428 
62 117 1434 
62 117 1435 
63 117 1422 
198 117 1197 
229 117 1312 
230 117 1179 
243 117 1272 
55 118 1236 
56 118 1166 
61 118 1191 
65 118 1755 
57 119 1213 
57 119 1176 
58 119 1253 
62 119 1365 
62 119 1331 
63 119 1457 
63 119 1251 
66 119 1842 
66 119 1468 
59 120 1489 
59 120 1387 
60 120 1218 
60 120 1224 
61 120 1214 
61 120 1440 
62 120 1198 
64 120 1240 
205 120 3601 
205 120 1168 
206 120 3727 
207 120 4089 
208 120 2128 
208 120 1160 
56 121 1293 
57 121 1183 
59 121 1371 
59 121 1347 
61 121 1314 
64 121 1346 
207 121 3562 
208 121 3845 
209 121 3534 
210 121 1201 
210 121 1405 
83 122 1794 
206 122 1259 
207 122 1161 
83 123 3550 
+0

相似/有用:https://stackoverflow.com/questions/38550379/how-to-assign-colors-to-each-value-in-scatter-function-gnu-octave/38557802#38557802 –

+0

請考慮接受其中一個答案,謝謝! – thewaywewalk

回答

0

這是一個相當粗糙的功能,但我認爲它實現了類似的結果,你想要什麼。

  1. 循環遍歷每個點,計算一些公差距離內的點數。
  2. 繪製這些點,使用附近點的計數作爲顏色的縮放比例。

代碼:

a = rand(1000,3);  % Create random matrix, use your data here 
n = zeros(size(a,1),1); % Set up array for number of nearby points 
tol = 0.2;    % Tolerance for (squared) distance to count as "nearby" 
sz = size(a,1);   % Shorthand for size of data 
% Loop over every point 
for ii = 1:sz; 
    dists = sum((repmat(a(ii,:), sz, 1) - a).^2, 2); % Get standard Euclidean distance 
    n(ii) = nnz(dists < tol); % Count number of points within tolerance 
end 
% Plot, colouring by an nx3 RGB array, in this case just 
% scaling the red and having no green or blue. 
scatter3(a(:,1), a(:,2), a(:,3), [], [n./max(n), zeros(numel(n),2)], 'filled'); 
grid on; 

輸出:

plot

+0

謝謝你的代碼!當我輸入數據並嘗試運行它時,我得到錯誤消息:「下標索引必須是真正的正整數或邏輯」當我的代碼運行到這裏時:dists = sum((repmat(a(ii,:) ,sz,1) - a)。^ 2,2);這是我的數組問題嗎? – Anonymous

+0

我剛剛用隨機數據嘗試了代碼,它仍然給我「下標索引必須是真正的正整數或邏輯」錯誤。我認爲這與總和命令有關......但我不確定。 – Anonymous

+0

你完全複製了for循環嗎?你是否正確設置了'sz'的值?如果我完全按原樣運行我的代碼,則不會出現該錯誤,嘗試在出現錯誤時獲取「ii」的值。 – Wolfie

1

在我的方法,我用一個閾值係數T,以確定其他點有多少距離的計算考慮爲每個點。 T = 1意味着每個點都計算到所有其他點的平均距離,T = 0.01表示每個點計算到其他點的最接近1%的平均距離。

figure 

%// example data 
[X,Y,Z] = sphere(15); 
x = [0.1*X(:); 0.4*X(:); 0.7*X(:)]; 
y = [0.2*Y(:); 0.5*Y(:); 0.8*Y(:)]; 
z = [0.3*Z(:); 0.6*Z(:); 0.9*Z(:)]; 
D = [x(:), y(:), z(:)]; 
N = numel(x); 

%// calculation of color vector 
[n,m] = ndgrid(1:N,1:N); 
%// euclidian distance of each point to every other point 
X = arrayfun(@(a,b) sum((D(a,:) - D(b,:)).^2), n, m); 

%% subplot 1 
%// threshold factor 
T = 0.01; 

%// sort distances of points 
Y = sort(X,2); 
%// calculate average distance of the closest T% of all points 
Z = mean(Y(:,2:ceil(N*T)),2); 

%// plot 
subplot(121) 
scatter3(x,y,z,20,Z,'filled'); 
title('T = 0.01') 
colormap 
colorbar 

%% subplot 2 
%// threshold factor 
T = 1; 

Y = sort(X,2); 
Z = mean(Y(:,2:ceil(N*T)),2); 

%// plot 
subplot(122) 
scatter3(x,y,z,20,Z,'filled'); 
title('T = 1') 
colormap 
colorbar 

enter image description here

+0

嗨,非常感謝你花時間幫助我!我想知道 - 這些線意味着什麼/他們做了什麼? x = [0.1 * X(:); 0.4 * X(:); 0.7 * X(:)]; y = [0.2 * Y(:); 0.5 * Y(:); 0.8 * Y(:)]; z = [0.3 * Z(:); 0.6 * Z(:); 0.9 * Z(:)]; – Anonymous

+0

@Anonymous它只是示例數據,用你的數據替換'x','y'和'z'! – thewaywewalk

+0

嗨,當我使用您的示例數據時,一切都很好。但是,當我使用自己的數據時,當我到達命令@(a,b)sum((D(a,:) - D(b,:))。^ 2)時,會出現以下錯誤消息:指數必須是真正的正整數或邏輯。你有什麼想法可以解決這個問題嗎?非常感謝! – Anonymous