Matlab 使用imagesc时添加图例,包括白色表示NaN

Matlab 使用imagesc时添加图例,包括白色表示NaN,matlab,matlab-figure,legend,Matlab,Matlab Figure,Legend,我有一个35x43的数据矩阵,数值范围为1-6,还有很多N 我希望南是白色的,每个数字都是不同的颜色。我需要一个带有6种不同颜色和标签的图例 我可以通过以下代码实现大部分功能,但是图例中的颜色与图中的颜色不匹配。请参见下面的代码 figure(6) subplot(1,2,1) imagesc(lut) title('Highest Weighted Predictor variable for each Pixel') ylabel('Longitude') xlabel('Latit

我有一个35x43的数据矩阵,数值范围为1-6,还有很多N

我希望南是白色的,每个数字都是不同的颜色。我需要一个带有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]

要将
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'})

我建议使用带有单个刻度的
色条的替代解决方案:

%// 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'}