Default Text Size in Legends

88 views (last 30 days)
Giovanni
Giovanni on 26 Apr 2012
Answered: Mike Garrity on 25 Mar 2016
Hi everybody, My problem is the following.
I need to change the default size of the text displayed in the figure legends, setting it, for example, to 22. Therefore I have inserted in the startup file, where I define my default graphics preferences, the following command:
set(0,'DefaultLegendFontSize',22);
Curiosly, I didn't get any error, but the command does not work, that is, Matlab continues to produce legend whose labels have the same font size (26) that I have set for x/y-axis label using set(0,'DefaultTextFontSize',26);
I have read that the command set(0,'DefaultTextFontSize',26); should set the font size also of legend. For this reason I have written in my startup file the set(0,'DefaultLegendFontSize',22); AFTER set(0,'DefaultTextFontSize',26); hoping in this way to 're-write' for labels the general option set for text.
A strange thing is that set(0,'DefaultLegendFontSize',22);, which does not work, does not produce any error message..
Does anyone know how to change the default size of text in labels? I know how to change it once created each figure using for example
h_legend=legend('a','b'); set(h_legend,'FontSize',22);
but I would prefer to change it by default
Thanks in advance, Giovanni Betti Beneventi
  1 Comment
Sanket Diwale
Sanket Diwale on 10 Feb 2016
Matlab 2015b Help on Legend Properties says the following about legend text size being scaled according to the axes size:
"Font size, specified as a scalar value greater than zero in point units. The default value is 9 points. If you change the axes font size, then MATLAB automatically sets the legend font size to 90% of the axes font size. If you manually set the legend font size, then changing the axes font size does not affect the legend."
I am guessing this is somehow preventing the default legend text size to change even though the property can be accessed and changed through set(0,'DefaultLegendFontSize',22);
Mathworks needs to clarify how this default property can be changed such that it works.

Sign in to comment.

Answers (2)

Mike Garrity
Mike Garrity on 25 Mar 2016
A couple of things going on here.
First, before R2014b a legend was an axes, so they shared the same defaults. Starting in R2014b, legend gets its own defaults.
Second, as the doc says, legend will automatically set the FontSize to 90% of the FontSize of the associated axes. When you set the FontSize on a legend, you override this. You do that because you're actually setting the FontSize property AND setting the FontSizeMode property to manual. To do this with defaults, you need to do exactly the same thing.
figure('DefaultLegendFontSize',22,'DefaultLegendFontSizeMode','manual')
plot(1:100)
legend show
But again, that's only for R2014b or later.

Sean de Wolski
Sean de Wolski on 26 Apr 2012
A legend is just a form of an axes, i.e:
peaks;
h = legend('peaks');
get(h,'type')
And thus you would need to set the axes' fontsize. This might have adverse affects elsewhere.
I would take another approach and write your own wrapper for legend that manually sets the fontsize. E.g. something along the lines of:
function h = mylegend(varargin)
%Wrapper for legend that will set it to size 22.
h = legend(varargin{:})
set(h,'fontsize',22)
  1 Comment
Chibuzo Nnonyelu
Chibuzo Nnonyelu on 25 Mar 2016
get(legend, 'type')
returns
legend
The defaultLegendFontSize property can not be changed. It is always 90% of the AxesFontSize.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!