使用imagesc时添加图例,包括用于NaN的白色 [英] Adding a legend when using imagesc, including white for NaN

查看:118
本文介绍了使用imagesc时添加图例,包括用于NaN的白色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个35x43的数据矩阵,其价位从1-6到许多NaN.

I have a 35x43 matrix of data with vales ranging from 1-6 and lots of NaNs.

我想让NaN为白色,而数字分别为不同的颜色.我需要一个带有6种不同颜色和标签的图例.

I want to NaNs to be white, and the numbers to each be a different colour. I need a legend with the 6 different colour and labels on it.

我可以使用以下代码来实现大部分功能,但是图例中的颜色与图中的颜色不匹配.参见下面的代码

I can achieve most of this with the following code, however the colours in the legend do not match the colours in the figure. See code below

    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值,我会

To display the NaN values as white I would use something like this. Then for your colormap, just use jet(6). Then the colors will match up just fine.

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

这篇关于使用imagesc时添加图例,包括用于NaN的白色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆