How to change symbol size and contour size in legend simultaneously?

102 vues (au cours des 30 derniers jours)
Jing Ci Neo
Jing Ci Neo le 20 Sep 2020
Commenté : Joscha Weinert le 5 Déc 2021
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!

Réponses (1)

Gouri Chennuru
Gouri Chennuru le 27 Sep 2020
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 commentaire
Joscha Weinert
Joscha Weinert le 5 Déc 2021
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.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by