How do I change the font in my legend? (possible bug)

135 views (last 30 days)
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 Comments
dpb
dpb on 23 Nov 2016
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.
Richard Newton
Richard Newton on 23 Nov 2016
I've added the figures that are generated by the code. MATLAB version is 2015b.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 23 Nov 2016
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 Comments
dpb
dpb on 23 Nov 2016
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
Sam Walder on 31 Aug 2018
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.

Sign in to comment.

More Answers (1)

Jan
Jan on 23 Nov 2016
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 Comments
dpb
dpb on 23 Nov 2016
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.
Richard Newton
Richard Newton on 23 Nov 2016
Edited: Richard Newton on 23 Nov 2016
Ok, I understand. The figure 1 code was just an example to show the case where it works (i.e. to show I've not done something silly like mistyped Timse Nwe Rmoan etc.). The behaviour is the same if you run only the code to plot figure 2. Just run the following for diagnostic purposes:
% 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');
One thing I didn't mention is that when this code is run, lgnd.FontName (or get(lgnd,'FontName') for old-school MATLABers) returns "Times New Roman", but doesn't display Times New Roman.
Interesting that the phenomenon is not seen in R2009a.

Sign in to comment.

Tags

Products

Community Treasure Hunt

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

Start Hunting!