How to change the symbol size of the legend

49 次查看(过去 30 天)
figure
cmap = colormap(cool(4));
hold on
LL(1) = scatter(nan, nan,75, cmap(1,:), "square", 'filled');
hold on
LL(2) = scatter(nan, nan,75, cmap(2,:), "square", 'filled');
hold on
LL(3) = scatter(nan, nan,75, cmap(3,:), "square", 'filled');
hold on
LL(4) = scatter(nan, nan,75, cmap(4,:), "square", 'filled');
hold on
LL(5) = scatter(nan, nan,200, "black", "d");
hold on
LL(6) = scatter(nan, nan,200, "black", "o");
legend(LL, {'1', '2','3', '4','5', '6'},'Location','eastoutside')
set(gca,'fontweight','bold','FontSize',30)
Hello how to change the size (make them bigger) of the "square", "d", and "o"(in front of numbers) in the legend.

回答(1 个)

Rik
Rik 2023-1-27
编辑:Rik 2023-1-27
This is only possible by increasing the size of the markers themselves in the plot. If you don't want that, you will need to create dummies with NaN.
Edit:
After some experimentation, I found that there is a maximum size of marker that legend will show, which is about 10. Note that the size argument for a line object (i.e. the result of plot) describes the length, while the size argument for scatter describes the area, hence the need to square to get the same visual size.
For scatter, the legend will always have the same size, while for plot the size is the same as what you see in the axis.
cmap = colormap(cool(10));
hold on
for n=1:size(cmap,1)
sz=n*2;
plot(sz, sz,'d','MarkerFaceColor',cmap(n,:),'MarkerEdgeColor', cmap(n,:), ...
'MarkerSize',sz,...
'DisplayName',sprintf('plot size=%d',sz));
scatter(sz/2, sz, sz^2, cmap(n,:), 'square', 'filled',...
'DisplayName',sprintf('scatter size=%d',sz^2))
end
hold off
axis([0 sz*1.5 0 sz+1])
legend('Location','southeast')
The end result is that you will have to create your own implementation of the legend with a second axis. That way you are no longer limited by the maximum size.
  1 个评论
Miraboreasu
Miraboreasu 2023-1-27
编辑:Miraboreasu 2023-1-27
LL(1) = scatter(nan, nan,75, cmap(1,:), "square", 'filled');
Thanks for the reply, but I have this as dummy with nan, and size 75, when I change 75 to 100, the size of the markers won't change @Rik

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

标签

产品


版本

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by