Make squared in legend instead of lines Matlab

我们两清 提交于 2019-11-29 14:51:04

Use nan to create invisible data (thanks @matlatbgui), and set L with all needed properties for no line and filled square markers:

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

and you don't need your line:

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

The square on the upper-left corner is obviously due to set(L(:),'Marker','s') which draws a square at the start and end points of the lines, at [1, 1]. Instead of changing the 'Marker', if you increase the 'LineWidth', you get much better results with:

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

With this output:

So if you are not restricted to make squares, I believe wide rectangles are better flags for color.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!