Make a square in the legend instead of Matlab lines

Separation of land use types

I have the following code that displays a “map” using imagesc and provides a legend, see the output attached.

I am trying to replace the lines in the legend with solid squares. My attachés are very far from lines and advertising hollow squares (including a random square in the upper left corner of the picture)

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]
+4
source share
2 answers

Use nanto create invisible data (thanks @matlatbgui) and set Lwith all the necessary properties without string 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 do not need your line:

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

legend

+4
source

, , 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

, , , .

+1

Source: https://habr.com/ru/post/1657240/


All Articles