Change the symbols size in a figure legend
Asked Answered
B

3

5

Is there an option to change the symbol size in the legend that Matlab creates? I only want to increase the symbol size in the legend. I have used 4 scatters of 3 points each.

Battologize answered 26/2, 2015 at 11:52 Comment(0)
S
13

Matlab R2014b or newer (HG2)

The organization of the elements in the legend is now different. The following works:

plot(1:10, 'o-'); 
hold on
plot(5:12, 'r*--'); %// example plots
[~, objh] = legend({'one plot', 'another plot'}, 'location', 'NorthWest', 'Fontsize', 14);
%// set font size as desired
objhl = findobj(objh, 'type', 'line'); %// objects of legend of type line
set(objhl, 'Markersize', 12); %// set marker size as desired

enter image description here

Matlab R2014a or older

To increase font size: get handles to all legend's children of type 'text', and set their 'Fontsize' property to the desired value.

To increase marker size: get handles to all legend's children of type 'line', and set their 'Markersize' property to the desired value.

plot(1:10, 'o-'); 
hold on
plot(5:12, 'r*--'); %// example plots
h = legend('one plot', 'another plot', 'location', 'NorthWest'); %// example legend
ch = findobj(get(h,'children'), 'type', 'text'); %// children of legend of type text
set(ch, 'Fontsize', 14); %// set value as desired
ch = findobj(get(h,'children'), 'type', 'line'); %// children of legend of type line
set(ch, 'Markersize', 12); %// set value as desired

enter image description here

Sloane answered 26/2, 2015 at 12:11 Comment(4)
First of al' thank you. I've tried to do this' but it increases the letters of the text that in the legend- the font size, but not changing the symbols syze..Battologize
If larger font size is not what you want, what is it? Larger markers maybe?Sloane
@Veridian Can you be a little bit more specific? What exactly didn't work? Did you get an error, and if so which one? Was a wrong plot produced, and if so what was wrong about it?Sloane
@LuisMendo, the marker size didn't change. no error was produced.Phosphoprotein
C
6

In case anyone is like me and comes here looking for a way to change legend marker size for scatter plots: rather than objects of type 'line' as for line plots you need objects of type 'patch' (2017b).

objhl = findobj(objh, 'type', 'patch'); % objects of legend of type patch
set(objhl, 'Markersize', 12); % set marker size as desired
Crisp answered 22/5, 2018 at 19:46 Comment(2)
This is valid at least as far back as 2015, too.Fastigiate
Worked on 2023b, thank you.Besom
Q
0

In my case I need to plot a multi-column legend in MATLAB R2019b (using the "NumCloumns" property), and I need to change the marker size in legend. Unfortunately these two requirements cannot be simultaneously fulfilled. MATLAB will ignore the "NumColumns" argument in you get two returns from the legend function.

I end up ploting some very large dots with the same color as the experiment data outside the axis limit and show legend of them. This works well though it is a dirty solution.

Quaver answered 21/1, 2020 at 7:2 Comment(1)
I can see an answer in there. Please rephrase in order to avoid the impression of asking your own question. I.e. reduce the emphasis on "my case" and make the answering part more prominent.Foretopmast

© 2022 - 2024 — McMap. All rights reserved.