MATLAB Answers

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

136 views (last 30 days)
Jon on 8 Jun 2017
Edited: Adam Danz on 5 May 2019
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.


Show 1 older comment
Ryan Alcantara
Ryan Alcantara on 9 Oct 2017
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?
Wonmo Sung
Wonmo Sung on 2 Apr 2018
I tried above in MATLAB 2018a. It seems like this issue is already fixed.
pcidreir on 1 May 2019
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)

Adam Danz
Adam Danz on 1 May 2019
Edited: Adam Danz on 5 May 2019
The only currently supported output to legend() is the legend handle (link to docmentation). Strangely, when the undocumented, second output is included, it interferes with the ability to change the fontsize of the legend (I'm using r2019a).
Successful example
lh = legend('First', 'Second', 'Third');
lh.FontSize = 14; %or set(lh, 'FontSize', 14)
Buggy example
[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.


Sign in to comment.

Community Treasure Hunt

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

Start Hunting!