2017-06-01 79 views
1

我有一些向量具有已定義的位置和方向。我可以通過使用下面的代碼顯示它們在空間:按密度指定顫抖矢量顏色

theta = [pi/2,-pi/2,pi/2,pi/2,pi/2,pi/2,pi/2]; 
r = 0.25; % magnitude (length) of arrow to plot 
x = [4,3.5,3.75,4.5,8,10,12]; y = [8.5,8.2,8.3,8,9,10,8]; 
u = r * cos(theta); % convert polar (theta,r) to cartesian 
v = r * sin(theta); 
h = quiver(x,y,u,v,'linewidth',2); 
set(gca, 'XLim', [2 15], 'YLim', [4 15]); 

正如從圖像清晰,在某些地區箭頭的數量比其他地方更多。我想按顏色顯示箭頭,其中每種顏色代表箭頭的密度。

任何人都可以幫助我做到這一點?如果存在顯示局部密度的連續背景色,這也是一個很好的解決方案。

+0

正如EBH在前面的問題中所說的那樣,當使用'顫動器'時不能指定單獨的箭頭顏色。會像一個基礎的等高線圖(看到[在這個漸變的例子](https://uk.mathworks.com/help/matlab/ref/quiver.html#bt28nw6-1))可以很好地說明密度,如果顏色是由密度定義的? – Wolfie

+0

是的,如果顏色是由密度定義的,一切正常,問題就解決了@Wolfie –

+0

如果有背景顏色顯示密度而不是線條,它也是完美的@Wolfie –

回答

2

編輯:下面是根據點的密度爲圖的背景着色的一些選項。我正在編輯這個到我的答案的頂部,因爲它實際上回答你的問題 - 單獨着色基於密度的quiver箭頭!

x = rand(200,1)*10; y = rand(200,1)*10; % Set up random points 
r = 1; u = r * cos(x); v = r * sin(y); % Quiver directions 
colormap winter; c = colormap;   % Set colourmap and assign to matrix 
% Get density of points broken into a 10x10 grid 
[n,~,~,binX,binY] = histcounts2(x,y,[10,10]);  
% Get colour based on histogram density and chosen colormap colours 
col = c(ceil(n(sub2ind(size(n), binX, binY))/max(n(:))*size(c,1)),:); 
figure; hold on; 
% Each quiver point must be plotted individually (slow!) because colours can 
% only be applied to individual quivers. This could be sped up by plotting 
% all of the same colour at once. 
for ii = 1:size(x,1); 
    quiver(x(ii),y(ii),u(ii),v(ii),0,'color',col(ii,:)); 
end 

輸出:

quiver colours

注意:不像下面的例子,因爲你需要它太返回窗口索引,你不能使用hist3。您可以嘗試使用this File Exchange函數來實現相同的結果(未經測試)。


下面是使用hist3得到密度(在這個例子中我使用10×10網格,作爲調用hist3時指定)的選項。然後使用pcolor來顯示密度,並使用shading interp來平滑顏色。

注:hist3需要統計& ML工具箱,如果你有Matlab的2015B或更新您可以改用標準函數histcounts2(x,y)

% Generate points and quiver directions 
x = rand(200,1)*10; y = rand(200,1)*10; 
u = r * cos(x); v = r * sin(y); 
% Get density of points, format for input to pcolor 
n = hist3([x,y],[10,10]); % Get density of points broken into a 10x10 grid 
colx = linspace(min(x),max(x),size(n,1)+1); 
coly = linspace(min(y),max(y),size(n,1)+1); 
n = n'; n(size(n,2)+1,size(n,1)+1) = 0; 
% Plot 
figure 
pcolor(colx,coly,n) % Density plot 
hold on; colorbar; % Hold on for next plot and show colour bar key 
quiver(x,y,u,v,'r') % Quiver plot 
shading interp  % Smooth plot colours 

輸出:

quiver


編輯:使色彩更加柔和

您可以控制使用colormap的顏色。這可能是默認設置之一,或者您可以創建一個RGB三元組的自定義地圖,並擁有您想要的任何顏色!下面是一個例子,只需在上面的代碼的最後調用colormap bone;

bone map

在自定義顏色的地圖,你可以使色彩更加柔和/少形成鮮明對比。

此外,您可以使用caxis來縮放圖的顏色軸!只需撥打

caxis([0,2*max(n(:))]); 

在上述代碼的最後加倍最大色彩圖值。你可以調整2,從而獲得所需的結果:

muted

+0

:)謝謝。有沒有辦法讓背景顏色對密度不太敏感?因爲輸出中有很多顏色,所以會令人困惑 –

+0

對不起,我怎樣才能讓它對密度更敏感?在我模擬的情況下,當我使用你的代碼時,背景全是黑色 –

+0

使用哪種方法?您可以增加採樣分辨率(在hist3([x,y],[k,k]);'中使用更大的'k')以獲得更高的保真度,或者如已經顯示的那樣更改色階。其餘取決於你的數據... – Wolfie

1

這看起來方式不太花哨,但指定一定數量的x軸的垃圾箱的箭頭顏色箭頭數量的功能

close all; 
cm=colormap; 

theta = [pi/2,-pi/2,pi/2,pi/2,pi/2,pi/2,pi/2]; 
r = 0.25; % magnitude (length) of arrow to plot 
x = [4,3.5,3.75,4.5,8,10,12]; y = [8.5,8.2,8.3,8,9,10,8]; 

[n,c]=hist(x,5); %count arroes in bins 

u = r * cos(theta); % convert polar (theta,r) to cartesian 
v = r * sin(theta); 

figure;hold on 

for ii=1:numel(n) %quiver bin by bin 
if n(ii)>0 
if ii==1 
wx=find(x<(c(ii)+(c(ii+1) - c(ii))/2)); %Which X to plot 
elseif ii==numel(n) 
wx=find(x>c(numel(n)-1)); 
else 
wx=find((x>(c(ii)-(c(ii)-c(ii-1))/2)).*(x<(c(ii+1)-(c(ii+1)-c(ii))/2))); 
end 

indCol=ceil((size(cm,1)*n(ii)-0)/max(n));%color propto density of arrows %in this bin 
col = cm(indCol,:);%color for this bin 

h = quiver(x(wx),y(wx),u(wx),v(wx),0,'linewidth',2,'color',col); 

end 
end 

colorbar 
caxis([0 max(n)]) 
+0

有沒有一種方法可以使矢量的顏色對密度更敏感? –

+0

有更多的向量XD或玩色彩地圖 – shamalaia

+0

這是一個非常好的答案。唯一的問題是顏色以圖層x軸的形式變化。請使用'histc'在下面的答案 –