How do I adjust legend fontsize after changing legend marker size?

There seems to be some kind of glitch in my Matlab program (Using R2016b). I am following the steps to change the marker size from this this URL. I can use their example and it will indeed change the marker size:
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
However, unlike their example, the fontsize in my image stays fixed. If I try to change fontsize through normal methods, for example,
[~, objh] = legend({'one plot', 'another plot'}, 'Fontsize', 25);)
it will change the box size of the legend, but not the font, as seen in the image below. The font size only becomes fixed when I designate a second output variable in the legend command, in this case, by assigning objh1. So if I just wrote
leg = legend({'one plot', 'another plot'}, 'Fontsize', 25);)
then it would change the font as expected. Because the example in my original URL changes font size correctly, I think this is a problem with my MATLAB software. Is it a version problem or something unusual on my end? I did reinstall the software and still have the same problem.

4 Comments

I have been able to reproduce the same on my machine as well. This issue has been fixed in the current version of MATLAB and should provide you with an increased size.
I'm running 2017a and have the same issue. setting 'FontSize' will change the box size of the legend, but not the font. Is there a work around or is this fixed with 2017b?
I tried above in MATLAB 2018a. It seems like this issue is already fixed.
I'm actually experiencing a similar issue on a subplot. When I increase the lengend size, the box increases, but the text font remains the same. I'm using matlab 2018a

Sign in to comment.

Answers (1)

The only currently supported output to legend() is the legend handle (link to docmentation). Starting in R2024b, calling legend with multiple outputs will throw a warning.
When the undocumented, second output is included, it interferes with the ability to change the fontsize of the legend (using r2019a).
Successful example
plot(magic(3))
lh = legend('First', 'Second', 'Third');
lh.FontSize = 14; %or set(lh, 'FontSize', 14)
Buggy example
plot(magic(3))
[lh, lineObj] = legend('First', 'Second', 'Third');
lh.FontSize = 14; %or set(lh, 'FontSize', 14)
If the fontsize is specified in the call to legend, the function behaves as expected.
[lh, lineObj] = legend('First', 'Second', 'Third', 'FontSize', 14); % OK
Other differences when the second output is included are
  • AutoUpdate property is set to 'off' rather than 'on'
  • the UIContextMenu is an empty graphics place holder rather than the default legend context menu.

7 Comments

I am using 2018a.
[lh, lineObj] = legend('First', 'Second', 'Third', 'FontSize', 14);
will make error. I find out that 'Fontsize' cause the error, when I want two output
You must use curly braces {} wrapping around the legend entries:
[lh, lineObj] = legend({'First', 'Second', 'Third'}, 'FontSize', 14)
You can control the FontSize through the lineObj, like so:
plot(magic(3))
[lh, lineObj] = legend({'First', 'Second', 'Third'});
for i = 1:3
lineObj(i).FontSize = 14;
end
the problem is that the legend box does not update its size:
You can resize the legend box by updating the FontSize of lh too, but it also makes the the icons bigger and causes the text to bleed out:
plot(magic(3))
[lh, lineObj] = legend('First', 'Second', 'Third');
lh.FontSize = 14;
for i = 1:3
lineObj(i).FontSize = 14;
end
@Leone Campos, as my answer indicates, requesting more than 1 output argument from legend is discouraged and leads to bugs when setting the legend font size.
  1. Look at the legend in your example and compare it to the legend in the example from my answer. Notice that the legend box does not update in your demo.
  2. Your for-loop has a fixed iteration value of 1:3 because the first three objects in lineObj are legend text but legends from other figures may have a different arrangement of objects in which case 1:3 may not index all legend text object.
Key point: Avoid requesting more than 1 output to legend.
Yeap, I agree with all of your points.
The problem is that sometimes we want to suffer by innovating and creating a whole new mess inside the legend element. In these cases, we are forced to explore the unstable side of the world called MATLAB.
Starting in R2024b, calling legend with multiple outputs will throw a warning. It will continue to function as it has previously.

Sign in to comment.

Asked:

Jon
on 8 Jun 2017

Edited:

on 7 Oct 2024

Community Treasure Hunt

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

Start Hunting!