필터 지우기
필터 지우기

How to change in marker size in the global legend?

조회 수: 38 (최근 30일)
Zhe Dong
Zhe Dong 2024년 8월 14일 17:36
편집: Zhe Dong 2024년 8월 15일 10:09
Hi,
I'm making a plot containing a few subplots using the function tiledlayout, and I created a global legend using the code
leg = legend({'A','B','C'})
leg.Layout.Tile = 'North'
However with this I can't use the previous method to change the marker size in the legend, because it requirs two outputs from the legend, and it will override the previous code.
[~,icons] = legend({'A','B','C'})
icons1=findobj(icons,'type','patch');
set(icons1,'MarkerSize',15,'Linewidth',1.5);
Anyone know the workaround of this? many thanks!

채택된 답변

Voss
Voss 2024년 8월 14일 17:45
[leg,icons] = legend({'A','B','C'});
leg.Layout.Tile = 'North';
icons1=findobj(icons,'type','patch');
set(icons1,'MarkerSize',15,'Linewidth',1.5);
  댓글 수: 1
Zhe Dong
Zhe Dong 2024년 8월 15일 9:36
편집: Zhe Dong 2024년 8월 15일 9:37
Thanks, this actually works! but with this approach I can only define the legend properties within the legend brackets, whereas the dot notation will not be responded, but anyway it worked for me!
[leg,icons] = legend({'A','B','C'}, 'FontSize',15); % this way you can adjust the fontsize
leg.FontSize = 15; % this doesn't work, the fontsize in legend is still displayed as default

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Benjamin Kraus
Benjamin Kraus 2024년 8월 14일 17:50
편집: Benjamin Kraus 2024년 8월 14일 17:51
The only documented way to modify the legend items independently from the contents of the axes is to create "dummy" objects with all NaN data. For example, this code uses copyobj to duplicate the "real" line objects, then uses set to change the XData and YData to NaN, so that the duplicate items don't show up in the main axes. When you create the legend, you need to specify which three line objects (the duplicates) you want in the legend. Now you can set properties on those duplicated objects and they will be represented in the legend but not in the axes.
This same strategy works whether you are using one legend in the axes, or using a tiledlayout to create a global legend.
pReal = plot(magic(3),'.-');
pNaN = copyobj(pReal, pReal(1).Parent);
set(pNaN,"XData",NaN,"YData",NaN);
h = legend(pNaN, {'A', 'B', 'C'});
set(pNaN, 'MarkerSize', 30);

Matt J
Matt J 2024년 8월 14일 18:21
편집: Matt J 2024년 8월 14일 18:26
You will probably have to use legendflex from the file exchange,
This means falling back to subplot, I'm afraid.
for i=1:2
subplot(20,2,6+i:2:40);
h=plot(rand(5,3));
[h.Marker]=deal('x','o','v');
end
[leg,icons]= legendflex( {'A','B','C'},'ref',gcf, 'anchor', {'n','n'},'buffer',[0,-5]);
set(findall(icons,'type','line'),'MarkerSize',10,'Linewidth',2);

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by