Scale a Legend with the underlying Axis

조회 수: 10 (최근 30일)
Simon Kölbl
Simon Kölbl 2017년 9월 15일
댓글: Felix Schönig 2021년 1월 7일
Hi,
I'm trying to make a figure, of fixed size, with multiple subplots. The number of subplots, and thus the size of each can vary. Each subplot may have a dedicated legend. The Problem is, that the Legend always has the same size regardless of the size of the subplots. So if the subplots become small the legend is way to big.
I noticed that the legend scales with its FontSize, so I figured, if I set the FontUnits of the Legend to normalized it would to the trick. Though I am able to set the FontUnits property of the legend, its FontSize is still displayed in Pts and it won't scale when changing the subplot size.
So, is there a smart way to have the legend scale with the underlying Axis?
Best regards

답변 (1개)

Ramnarayan Krishnamurthy
Ramnarayan Krishnamurthy 2017년 9월 21일
The observation that the size of the legend scales with the Font Size is true. Is there a reason you would prefer not to change the Font Size in that case?
As in, assuming you have the handle to the legend of a subplot in 'a':
a.FontSize = 4;
Another approach would be to set the size of the legend as follows:
set(a ,'Position', [0.8 0.2 0.05 0.5])
This will require figuring out the appropriate x and y values for the position vector.
  댓글 수: 2
Simon Kölbl
Simon Kölbl 2017년 9월 22일
편집: Simon Kölbl 2017년 9월 22일
The thing is I don't want to set the FontSize manually. Consider this example:
figure
plot(randn(100,1));
hold on
plot(randn(100,1));
s1 = annotation('textbox', [0.2 0.2 0.1 0.1],'String', 'TESTEST', 'FitBoxToText', 'on');
l = legend('TEST1', 'TEST2');
The FontUnits of both the Textbox and the legend are points by default. If I change the size of the figure with the mouse, the FontSize does not change, and thus the textbox and legendbox size does not as well.
Now I set the FontUnit properties of the textbox and legend to normalized:
s1.FontUnits = 'normalized';
l.FontUnits = 'normalized';
Now if I resize the figure, the FontSize of the textbox and its size scales accordingly, however, the FontSize of the legend stays the same during the resizing operation and thus doesn't scale.
The fontsize of the textbox, s1.FontSize, is displayed correctly in normalized units, while the fontsize of the legend, l.FontSize, is still displayed in points. The command
l.FontUnits = 'normalized';
does not seem to have any effect. If it would have, the legend would scale properly.
Felix Schönig
Felix Schönig 2021년 1월 7일
I am having the same problem. Did you find a solution?

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

카테고리

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