Default Text Size in Legends

조회 수: 78 (최근 30일)
Giovanni
Giovanni 2012년 4월 26일
답변: Mike Garrity 2016년 3월 25일
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
Sanket Diwale
Sanket Diwale 2016년 2월 10일
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.

댓글을 달려면 로그인하십시오.

답변 (2개)

Mike Garrity
Mike Garrity 2016년 3월 25일
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 2012년 4월 26일
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
Chibuzo Nnonyelu
Chibuzo Nnonyelu 2016년 3월 25일
get(legend, 'type')
returns
legend
The defaultLegendFontSize property can not be changed. It is always 90% of the AxesFontSize.

댓글을 달려면 로그인하십시오.

카테고리

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