How do I change the font in my legend? (possible bug)
Mostrar comentarios más antiguos
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 comentarios
dpb
el 23 de Nov. de 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
el 23 de Nov. de 2016
Respuesta aceptada
Más respuestas (1)
Jan
el 23 de Nov. de 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 comentarios
Richard Newton
el 23 de Nov. de 2016
dpb
el 23 de Nov. de 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
el 23 de Nov. de 2016
Editada: Richard Newton
el 23 de Nov. de 2016
Categorías
Más información sobre Legend en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!