2016-10-04 40 views
1

我有一個35x43的數據矩陣,其值範圍從1-6到許多NaN。在使用imagesc時添加圖例,包括NaN的白色

我想NaNs是白色的,每個數字是不同的顏色。我需要一個具有6種不同顏色和標籤的圖例。

我可以用下面的代碼實現大部分,但圖例中的顏色與圖中的顏色不匹配。見下文

figure(6) 
subplot(1,2,1) 
imagesc(lut) 
title('Highest Weighted Predictor variable for each Pixel') 
ylabel('Longitude') 
xlabel('Latitude') 
caxis([0, 7]) 
myColorMap = jet(7); 
myColorMap(1,:) = 1; 
colormap(myColorMap); 
M = jet(7); % Notice the 3, here and below 
hold on 
L = line(ones(7),ones(7)); 
set(L,{'color'},mat2cell(M,ones(1,7),3)) 
[legh,objh,outh,outm] = legend('First','Second','Location','Southeast'); 
set(objh,'linewidth',200); 
legend('Forest','Shrubland','Savanna','Grassland','Agricultural','Barron'); 
grid on 
ax = gca 
ax.GridAlpha = .2 
ax.XTick = [5 10 15 20 25 30 35 40]; 
ax.YTick = [5 10 15 20 25 30]; 
ax.XTickLabel = {'118^{o}E','123^{o}E','128^{o}E', '133^{o}E', '138^{o}E', '143^{o}E','148^{o}E', '153^{o}E'}; 
ax.YTickLabel = {'13^{o}S','18^{o}S','23^{o}S','28^{o}S','33^{o}S','38^{o}S'}; 
ax.TickLength =[0.0 0.0] 

回答

2

代碼要爲白色我會use something like this顯示NaN值。那麼對於你的色彩地圖,只需使用jet(6)。然後,顏色會匹配得很好。

lut = [1:6 NaN]; 

myColorMap = jet(6); 

imagesc(lut, 'AlphaData', ~isnan(lut)) 
colormap(myColorMap); 

L = line(ones(6), ones(6)); 
set(L, {'Color'}, num2cell(myColorMap, 2)) 

legend(L, {'Forest','Shrubland','Savanna','Grassland','Agricultural','Barron'}) 

enter image description here

1

我會使用colorbar與個別蜱提出一種替代的解決方案:

%// example data 
lut = randi(6,35,43); 
lut(1:23:end) = NaN; 

%// parts of your your code 
figure(6) 
% subplot(1,2,1) 
imagesc(lut) 
title('Highest Weighted Predictor variable for each Pixel') 
ylabel('Longitude') 
xlabel('Latitude') 
caxis([0, 7]) 
myColorMap = jet(7); 
myColorMap(1,:) = 1; 
colormap(myColorMap); 
M = jet(7); % Notice the 3, here and below 
hold on 

%// colorbar 
c = colorbar 
c.Ticks = (1:6)+0.5 
c.TickLabels = {'Forest','Shrubland','Savanna','Grassland','Agricultural','Barron'} 

enter image description here