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

81 views (last 30 days)
Jon on 8 Jun 2017
Commented: goldenair on 14 Jun 2021
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.
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.
  1 Comment
goldenair on 14 Jun 2021
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

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!