在传奇中设置平方而不是线条Matlab

时间:2016-10-10 06:14:38

标签: matlab plot matlab-figure legend

Ditribution of Land use Types

我有以下代码,用于绘制地图'使用imagesc,并提供图例,参见附件输出。

我正在尝试用实心方块替换图例中的线条。我的距离远远超出线条和空心方块(包括图中左上角的随机方块)

figure(6)
imagesc(lut)
title('Ditribution of Land use Types')
ylabel('Longitude')
xlabel('Latitude')
caxis([0, 7])
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'})
set(L(:),'Marker','s')
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 :(得分:4)

使用nan创建不可见数据(感谢@matlatbgui),并将L设置为所有需要的属性,不包含任何行和填充的方形标记:

% some arbitrary data:
N = 30;
lut = diag(1:N)*ones(N)+(diag(1:N)*ones(N)).';

% coloring settings:
caxis([0, 7])
myColorMap = jet(6);

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

% Setting the legend:
L = line(nan(6), nan(6),'LineStyle','none'); % 'nan' creates 'invisible' data
set(L, {'MarkerEdgeColor'}, num2cell(myColorMap, 2),...
    {'MarkerFaceColor'},num2cell(myColorMap, 2),... % setting the markers to filled squares
    'Marker','s'); 
legend(L, {'Forest','Shrubland','Savanna','Grassland','Agricultural','Barron'})

你不需要你的行:

set(L(:),'Marker','s')

legend

答案 1 :(得分:1)

左上角的方块显然是由set(L(:),'Marker','s')引起的,它在[1, 1]处的线的起点和终点画了一个正方形。如果您增加'Marker',而不是更改'LineWidth',您可以获得更好的结果:

L = line(ones(6), ones(6));
legend(L, {'Forest','Shrubland','Savanna','Grassland','Agricultural','Barron'})
set(L(:), 'LineWidth', 10)

使用此输出:

enter image description here

因此,如果您不限制制作正方形,我相信宽矩形是更好的颜色标志。