How do I change the font in my legend? (possible bug)
94 次查看(过去 30 天)
显示 更早的评论
The script below contains two methods of creating a legend. The first one, figure 1, gives the behaviour I want: the font is size 14, Times New Roman. However, in the second example, the font remains in the default typeface and size.
The only difference is the way the legend is initialized: in the first example the legend is initialized using "lgnd = legend(_);" with only one output varaible, whilst in the second example the legend is initialized using "[lgnd,icons,plots,txt] = legend(_);" with four output variables.
How do I change the font in the second example?
I have MATLAB 2015b.
% FIGURE 1 LEGEND IN TIMES NEW ROMAN
figure(1)
% generate some plots
plot([1 1 1 1],1:4,'.','MarkerSize',2)
hold on
plot([2 2 2 2],1:4,'.','MarkerSize',2)
plot([3 3 3 3],1:4,'.','MarkerSize',2)
plot([4 4 4 4],1:4,'.','MarkerSize',2)
hold off
lgnd = legend('1','2','3','4');
set(lgnd,'FontSize',14);
set(lgnd,'FontName','Times New Roman');
% FIGURE 2 LEGEND IN DEFAULT FONT
figure(2)
% generate some plots
plot([1 1 1 1],1:4,'.','MarkerSize',2)
hold on
plot([2 2 2 2],1:4,'.','MarkerSize',2)
plot([3 3 3 3],1:4,'.','MarkerSize',2)
plot([4 4 4 4],1:4,'.','MarkerSize',2)
hold off
[lgnd,icons,plots,txt] = legend('1','2','3','4');
set(lgnd,'FontSize',14);
set(lgnd,'FontName','Times New Roman');
Edit: Added the pictures as attachments to illustrate.
2 个评论
dpb
2016-11-23
Code (and results) looks identical to mee in either case???
Attach a figure with the issue and the code that generated it. One that works is of little, if any, additional value methinks. Also which version may be of importance if there really is some issue.
采纳的回答
Walter Roberson
2016-11-23
See http://www.mathworks.com/matlabcentral/answers/304258-retrieve-legend-handles-and-text#answer_235961
When you create a legend with four outputs in R2014b or later, it gets created differently, with an ItemText property that contains the text handles; if you create with one output then that property is empty and the text is handled internally somehow.
You can handle this together by using
temp = [lgnd; lgnd.ItemText];
set(temp, 'FontSize', 14)
set(temp, 'FontName', 'Times New Roman')
If the legend was built without the text output then lgnd.ItemText will be empty and temp will just be the lgnd object and in that case setting its properties works. If the legend was built with the text output then setting the lgnd properties appears to be ignored but does not hurt, and setting the text objects from the ItemText property works.
3 个评论
dpb
2016-11-23
Ugh-lee! is best can be said for it... :(
HG2 introduced a few nice features, but it broke a lot and has a lot of warts along with it...
Sam Walder
2018-8-31
Fantastic! I cannot see the 'ItemText' property in the legends properties, but asking for it explicitly gets it. I spent a long time lamenting that the legend seemed to be ignoring the text settings. This solves the issue.
更多回答(1 个)
Jan
2016-11-23
Nice! This did not happen in R2009a.
Try to narrow this down:
lgnd1 = legend('1','2','3','4');
...
[lgnd2, icons, plots, txt] = legend('1','2','3','4');
And now try to compare lgnd1 and lgnd2. (I do not have a modern Matlab version currently.)
3 个评论
dpb
2016-11-23
The intent isn't to put the two on the same figure but to not overwrite the first figure handle when creating the second so can programmatically query properties, etc., ...
I wondered if somehow that didn't play into the problem (that you reused the same variable and so somehow actually what you did was to rewrite the set instruction to the first legend instead, leaving the second unchanged.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Legend 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!