How to change symbol size and contour size in legend simultaneously?
134 次查看(过去 30 天)
显示 更早的评论
I wanted to make the marker size of scatter plot larger and the token size of the contour symbol in legend smaller, so I used the following code:
x=1:20;
a=rand(20,20);
figure; hold on
scatter(3,4)
contour(x,x,a)
h=legend('sp','cp');
h.ItemTokenSize = [10,5]; % Make both symbols smaller
To make the marker size of the scatter plot larger, I tried to get icons:
[h,icons]=legend('sp','cp');
h.ItemTokenSize = [10,5]; % Doesn't work anymore
icons = findobj(icons,'Type','patch');
icons = findobj(icons,'Marker','none','-xor');
set(icons(1),'MarkerSize',12); % To make scatter plot marker size larger
However, I lose control over the contour token size.
Can anyone please help me with this? Thanks!
0 个评论
回答(1 个)
Gouri Chennuru
2020-9-27
Hi Jing,
You can open MATLAB 2020a and execute the following code,
x=1:20;
a=rand(20,20);
figure;
hold on
scatter(3,4)
contour(x,x,a)
[h,icons]=legend('sp','cp');
h.ItemTokenSize = [10,5];
icons = findobj(icons,'Type','patch');
icons = findobj(icons,'Marker','none','-xor');
set(icons(1),'MarkerSize',12);
This code works fine in MATLAB 2020a.
If you are still facing the issue, provide additional information so that issue will be resolved.
Hope this Helps!
1 个评论
Joscha Weinert
2021-12-5
Hello,
I am facing the same issue.
My figure plot is based on an app designer program.
To adjust the length, I can use:
leg.ItemTokenSize = [x1,x2];
If I generate the Legend by
leg = legend(...);
So it works fine.
But I also want to adjust the marker size afterwards.Therefore I need the icons. So i use
[leg, icons] = legend(...);
h = findobj(icons,'type','patch');
set(h,'MarkerSize',1);
So I can adjust the marker size, but I can't change the length of the markers or lines in the legend no more.
Either way, one of my options gets lost.
@Gouri Chennuru: I tried your code and it worked. In another thread was mentioned, that app designer could be the problem. But maybe you have a solution. That would be really great. Maybe there is way to get the icons without this code:
[leg, icons] = legend(...);
Thank you in advance.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Legend 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!