How to set legend marker size

How do I change the marker size on the legend ? I can change the font size but not marker.
l = legend('Orientation', 'Horizontal', 'RNN (Ours)', 'SLIC', 'SEEDS', 'LSC', 'ERS', 'FH');
l.FontSize = 20;
%l.MarkerSize = 20; does not work
%l.markersize = 20; does not work
set(l,'Position', [0.4 0 0.2 0.2], 'Units', 'normalized');
%set(l,'MarkerSize', 20); does not work

Réponses (3)

Carl Witthoft
Carl Witthoft le 11 Avr 2019

5 votes

Best way: see Stackoverflow question 2871747
% thanks to , Luis Mendo and Lea
[~, objh] = legend({'one plot', 'another plot'}, 'location', 'NorthWest', 'Fontsize', 14);
%// set font size as desired
% note that even if you plot(x,y,'.') it's a "line" plot
objhl = findobj(objh, 'type', 'line'); %// objects of legend of type line
set(objhl, 'Markersize', 12); %// set marker size as desired
% or for Patch plots
objhl = findobj(objh, 'type', 'patch'); % objects of legend of type patch
set(objhl, 'Markersize', 12); % set marker size as desired

11 commentaires

Walter Roberson
Walter Roberson le 11 Avr 2019
If I recall correctly, R2018b / R2019a do not use line objects inside legends anymore.
I would need to recheck to be sure.
Abhijeet chausalkar
Abhijeet chausalkar le 3 Mai 2019
The command line with 'patch' worked for me in Matlab 2018a. I was really struggling to change the marker size using the code to plot a descent figure. Thanks Carl Witthoft.
Hi Carl,
I'm wondering if you could help me out with a very similar problem. Your solution actually worked for me for a while, and now it doesn't, even though the code stayed the same! For instance:
x = 1:10;
plot(x, 1*x, 'o')
hold on
plot(x, 2*x, 's')
h_legend = legend({'one','two'});
objhl = findobj(h_legend, 'type', 'line'); % objects of legend of type patch
set(objhl, 'Markersize', 99); % set marker size as desired
Whatever I type in place of the 99, makes no difference. If I change the 'line' into 'patch, that also makes no difference. The problem I guess comes from the fact that objhl is actually empty:
>> objhl = findobj(h_legend, 'type', 'patch')
objhl =
0x0 empty GraphicsPlaceholder array.
Any thoughts? Many thanks!
Walter Roberson
Walter Roberson le 4 Fév 2020
z8080: notice that in Carl's posted code, Carl takes the second output of legend(), whereas in your code, you take the first output of legend. legend() has some built-in backwards compatibility that is only used when you use at least two outputs on the legend() call. legend() with only one output uses different internal data structures that do not create line or patch objects.
Carl Witthoft
Carl Witthoft le 4 Fév 2020
Walter, thanks for reminding me of that bit.
I'll go back and re-test my code snippets.
z8080
z8080 le 5 Fév 2020
Ah yes, all clear now, thanks to both for clarifying!
Carl Witthoft
Carl Witthoft le 5 Fév 2020
Yep, my apologies for the rant. All is working here as well.
John
John le 19 Mai 2020
This functionality has gone away in release 2020a.
The legend command no longer returns objh - there is only one output from the function and it does not appear to contain (to my best ability searching) any properties of the patch objects for the bar graph that my legend annotates.
Has anyone any success modifiying the color patches in a legend in this version 2020a?
The code notes the change: This syntax is not supported
% [lgd,icons,plots,txt] = LEGEND(__) ...
% This syntax is not recommended. Some
% functionality is not supported. ...
Walter Roberson
Walter Roberson le 19 Mai 2020
I just tested in R2020a (Update 1), and [lgd,icons,plots,txt] still seems to work. I was able to activate a marker and change the marker size for a legend line by indexing into icons .
It might not be supported but it still works. For now.
I am working in Matlab 2019a, but Carl's code does not work in my case for a line plot.
objhl = findobj(objh, 'type', 'line'); %// objects of legend of type line
set(objhl, 'Markersize', 12); %// set marker size as desired
I mean, even if I change the value of MarkerSize, nothing changes.
Any update ?
Thanks.
Walter Roberson
Walter Roberson le 21 Juin 2020
Are you using legend() with at least two output arguments? If you only have one output for legend then legend works differently.

Connectez-vous pour commenter.

Akira Agata
Akira Agata le 16 Juil 2017

0 votes

How about changing the marker size of your plot? Here is an example.
plot(magic(4),'o','MarkerSize',10);
l = legend('a','b','c','d');

2 commentaires

Note that this only works up to a certain size. When using 15 instead of 10, the legend markers are clearly smaller than the plot markers.
plot(magic(4),'o','MarkerSize',15);
l = legend('a','b','c','d');
Monique
Monique le 24 Juil 2024
Modifié(e) : Monique le 24 Juil 2024
I just discovered something interesting (likely a bug). If you ask for 2 outputs, then the legend markers are actually the correct size. No idea why. (This is R2024a).
plot(magic(4),'o','MarkerSize',15);
[l1,l2] = legend('a','b','c','d');

Connectez-vous pour commenter.

Walter Roberson
Walter Roberson le 16 Juil 2017

0 votes

2 commentaires

Fernon Ejikhoudt
Fernon Ejikhoudt le 11 Juil 2018
I have the same problem and I'm using 2018a. Is there an answer to this. otherwise it almost makes the printed graphs useless because of the extremely tiny dots.
Walter Roberson
Walter Roberson le 11 Juil 2018
Did you experiment with Kelly's legendflex() ?

Connectez-vous pour commenter.

Question posée :

le 16 Juil 2017

Modifié(e) :

le 24 Juil 2024

Community Treasure Hunt

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

Start Hunting!

Translated by