MATLAB Answers

How to set legend marker size

460 views (last 30 days)
RuiQi
RuiQi on 16 Jul 2017
Commented: Walter Roberson on 19 May 2020 at 17:00
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

  0 Comments

Sign in to comment.

Answers (3)

Carl Witthoft
Carl Witthoft on 11 Apr 2019
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

  9 Comments

Show 6 older comments
Carl Witthoft
Carl Witthoft on 5 Feb 2020
Yep, my apologies for the rant. All is working here as well.
John
John on 19 May 2020 at 15:23
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 on 19 May 2020 at 17:00
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.

Sign in to comment.


Akira Agata
Akira Agata on 16 Jul 2017
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');

  0 Comments

Sign in to comment.


Walter Roberson
Walter Roberson on 16 Jul 2017

  2 Comments

Fernon Ejikhoudt
Fernon Ejikhoudt on 11 Jul 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 on 11 Jul 2018
Did you experiment with Kelly's legendflex() ?

Sign in to comment.

Sign in to answer this question.