編輯:下面是根據點的密度爲圖的背景着色的一些選項。我正在編輯這個到我的答案的頂部,因爲它實際上回答你的問題 - 單獨着色基於密度的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
輸出:
注意:不像下面的例子,因爲你需要它太返回窗口索引,你不能使用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
輸出:
編輯:使色彩更加柔和
您可以控制使用colormap
的顏色。這可能是默認設置之一,或者您可以創建一個RGB三元組的自定義地圖,並擁有您想要的任何顏色!下面是一個例子,只需在上面的代碼的最後調用colormap bone;
:
在自定義顏色的地圖,你可以使色彩更加柔和/少形成鮮明對比。
此外,您可以使用caxis
來縮放圖的顏色軸!只需撥打
caxis([0,2*max(n(:))]);
在上述代碼的最後加倍最大色彩圖值。你可以調整2
,從而獲得所需的結果:
正如EBH在前面的問題中所說的那樣,當使用'顫動器'時不能指定單獨的箭頭顏色。會像一個基礎的等高線圖(看到[在這個漸變的例子](https://uk.mathworks.com/help/matlab/ref/quiver.html#bt28nw6-1))可以很好地說明密度,如果顏色是由密度定義的? – Wolfie
是的,如果顏色是由密度定義的,一切正常,問題就解決了@Wolfie –
如果有背景顏色顯示密度而不是線條,它也是完美的@Wolfie –