2011-12-12 69 views

回答

15

NaN值獲得第一顏色從顏色表軸,默認情況下對應於(NaN以外)的最小值。您可以使用CAXIS函數更改最小值設置軸顏色限制的顏色。要將對比色分配給NaN值,可以爲NaN值添加特殊顏色作爲第一種顏色(1x3向量)。

我把你的榜樣,並提出了功能(有一些評論):

function [h hcb] = imagescwithnan(a,cm,nanclr) 
% IMAGESC with NaNs assigning a specific color to NaNs 

%# find minimum and maximum 
amin=min(a(:)); 
amax=max(a(:)); 
%# size of colormap 
n = size(cm,1); 
%# color step 
dmap=(amax-amin)/n; 

%# standard imagesc 
him = imagesc(a); 
%# add nan color to colormap 
colormap([nanclr; cm]); 
%# changing color limits 
caxis([amin-dmap amax]); 
%# place a colorbar 
hcb = colorbar; 
%# change Y limit for colorbar to avoid showing NaN color 
ylim(hcb,[amin amax]) 

if nargout > 0 
    h = him; 
end 

這裏CAXIS語句將彩色地圖不爲最小值amin的第一種顏色,但對amin-dmap。所以第一種顏色被專門分配給NaN。


試試這個功能有:

a=peaks; 
a(a < 0.5) = nan; 
imagescwithnan(a,hot,[0 1 1]) %# [0 1 1] is cyan 

test image - NaN color is hidden

如果你對此有何評論在功能上ylim聲明(可與附加的參數控制)這個NaN的顏色將在顏色表。

test image - NaN color is shown on the colorbar

2

有2個一般步驟使用多種顏色的圖是這樣的:

  1. 堆棧和你的舊的新的顏色表。
  2. 移動要映射到此新顏色圖的新值,以便範圍(新數據)與範圍(舊數據)的比率與nrow(舊顏色圖)的nrow(新顏色圖)相同。

這將成功將新數據映射到新色彩映射,同時將舊數據映射到舊色彩映射(即不會更改舊數據的顏色)。另外請注意,數據的副本被存儲在圖像中,所以我們可以在不改變原始值的情況下進行這種移動。

一個簡單的例子:

% Make image data 
img = -5:5; 

% Plot with original colormap 
figure 
imagesc(img); 
colormap(hot(8)) 

% Add in an NaN 
img(3) = NaN; 

% Make new colormap 
n = 8; 
cols = [0 0 1 %blue 
     hot(n)]; 

% Plot with new colormap 
figure 
h = imagesc(img); 
colormap([0 0 1; hot(8)]) 

% Scale data so that the range proportions match the colormap sizes 
cdata = get(h, 'CData'); 
img_range = range(cdata(:)); 
cdata(isnan(cdata)) = min(cdata(:)) - img_range/n; 
set(h, 'CData', cdata); 

enter image description here

enter image description here

0

這個問題首先顯示在我的搜索引擎,但我發現在this後一個更理想的答案,所以我想我將包括在這裏。

綜上所述,使用以下命令:

imagesc(Z,'AlphaData',~isnan(Z)) 

另外,請注意,pcolor默認忽略nan秒。