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

92 次查看(过去 30 天)
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 个评论
pcidreir
pcidreir 2019-5-1
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

请先登录,再进行评论。

回答(1 个)

Adam Danz
Adam Danz 2019-5-1
编辑:Adam Danz 2024-10-7
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 个评论
Afiq Azaibi
Afiq Azaibi 2024-10-7
Starting in R2024b, calling legend with multiple outputs will throw a warning. It will continue to function as it has previously.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by