使用imagesc时添加图例,包括NaN的白色

时间:2016-10-04 13:10:47

标签: matlab matlab-figure legend

我有一个35x43的数据矩阵,其数值范围从1-6到很多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 个答案:

答案 0 :(得分:2)

要将options mlogic; %macro delete_year_files_in_folder(folder); filename filelist "&folder"; data _null_; dir_id = dopen('filelist'); total_members = dnum(dir_id); do i = 1 to total_members; member_name = dread(dir_id,i); datestring = scan(member_name,4,'_'); month = input(substr(datestring,5,2),best.); day = input(substr(datestring,5,2),best.); year = input(substr(datestring,1,4),best.); date = mdy(month, day, year); if intnx('year', today(),-3,'S') > date %put _all_; then do; file_id = mopen(dir_id,member_name,'i',0); if file_id > 0 then do; freadrc = fread(file_id); rc = fclose(file_id); rc = filename('delete',member_name,,,'filelist'); rc = fdelete('delete'); end; %put _all_; rc = fclose(file_id); end; end; rc = dclose(dir_id); run; %mend; 值显示为白色,我会use something like this。然后,对于您的色彩映射,只需使用NaN。然后颜色就会匹配得很好。

jet(6)

enter image description here

答案 1 :(得分: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