2013-08-26 33 views
7

我使用hist3()函數來繪製點的密度。它創建一個網格並找出每個網格中的點數,然後創建該圖。但情節上的顏色是離散的。 有沒有一種選擇可以使這種分配順暢,即從一種顏色過渡到另一種顏色更平滑。現在網格中的所有單元格都有不同的顏色,從咧嘴到黃色,分佈不明顯。是否可以使hist3地塊更平滑?

我使用下面的代碼。

axis equal; 
colormap(jet); 
n = hist3(final',[40,40]); 
n1 = n'; 
n1(size(n,1) + 1 ,size(n,2) + 1) = 0; 
xb = linspace(min(final(:,1)),max(final(:,1)),size(n,1)+1); 
yb = linspace(min(final(:,2)),max(final(:,2)),size(n,1)+1); 
pcolor(xb,yb,n1); 

在此先感謝。

+1

檢查出['顏色表()'](http://www.mathworks.co.uk/help/matlab/ref/colormap。 HTML)。 – Oleg

+0

我使用夏季色彩地圖。我也嘗試了其他顏色映射,但它沒有幫助。我只是不想看到那些網格線,但想要從一個單元格到另一個單元格的平滑過渡。 – bordart

+0

使用[interp2](http://www.mathworks.fr/fr/help/matlab/ref/interp2.html)從直方圖創建插值曲面,然後顯示它。 – Bentoy13

回答

7

您可能需要使用從matlab file exchangegridfit功能。平滑效果來自內插(更多點指向圖)和充分利用可用顏色(此處爲colormap jet)。請注意0​​設置爲none,以便刪除黑線。

在此處使用時,需要輸出hist3(20x20矩陣)並對其進行插值(100x100)。然後使用surf繪製曲面。另外,您可以取消註釋camlight選項。

final = randn(1000,2)'; 
n = hist3(final',[20,20]); %binning 
figure('Color','w'); 

%your code with pcolor 
subplot(1,2,1); 
axis equal; 
colormap(jet); 
n1 = n'; 
n1(size(n,1) + 1 ,size(n,2) + 1) = 0; 
xb = linspace(min(final(:,1)),max(final(:,1)),size(n,1)+1); 
yb = linspace(min(final(:,2)),max(final(:,2)),size(n,1)+1); 
pcolor(xb,yb,n1) 


%density with gridfit function 
subplot(1,2,2); 
nb_interp_point = 100; 
[x,y] = meshgrid(1:size(n,1),1:size(n,2)); 
zgrid = gridfit(x(:), y(:), n, nb_interp_point, nb_interp_point); 
surf(zgrid,'EdgeColor','none') 
set(gca,'YDir','reverse'); 
view(-90,90); 
% camlight right 
% lighting phong 

下面是結果

enter image description here

+0

非常感謝。這是我想要做的。 – bordart

3

爲了擺脫網格線的使用下面的:

hchild=get(gca,'children'); 
set(hchild,'edgecolor','none') 

色彩映射表是米×3(RGB)陣列。您可以自由創建colormap。例如,您可以拉近你正在使用的colormap的範圍內,如下面的例子:

cmap=colormap(summer); 
range = [40:64]; % <-- here I am using a 64 element colorspace 
       % and narrowing the selection to the upper range 
nc = size(cmap,1); 

range = [range(1):(range(end)-range(1))/(nc-1):range(end)]; 
cmap(:,1)=interp1([1:nc],cmap(:,1),range); 
cmap(:,2)=interp1([1:nc],cmap(:,2),range); 
cmap(:,3)=interp1([1:nc],cmap(:,3),range); 
colormap(cmap) 
+0

不幸的是我不能讓這段代碼工作。你可以檢查一下嗎? – bordart

+0

@artalexan修正!錯過了-1 ... –

+0

非常感謝。這真的很有用!附:我剛剛刪除了這些括號,因爲它們沒有必要。 – bordart