Size of legend box

조회 수: 35 (최근 30일)
Ralf
Ralf 2011년 8월 5일
Hello,
I have a problem with the size of the box around the legend in a plot. I am using LaTeX commands in the legend, which Matlab does not know. This is not a problem, because I am exporting the figure with mlf2pdf and the code will interpreted properly. But, Matlab sets the sizes of the box around the legend including the unknown code. The result is, that in the PDF file at the right side of the legend box is a big white space. Trying to resize the box (that's where the factor 0.8 is) results in moving it.
The following code shows an example:
clf;
hfig = figure(1) ;
set(hfig,'units','centimeters') ;
x = linspace(0,1,101) ;
f = sin(2*pi*x) ;
plot(x,f) ;
hleg = legend('$N = 1 \dotsc 101$','Location','NorthEast') ;
fig_pos = get(hfig,'position') ;
set(hfig,'position',[fig_pos(1:2),10,10]) ;
leg_pos = get(hleg,'position') ;
set(hleg,'position'[leg_pos(1),leg_pos(2),...
leg_pos(3)*0.8,leg_pos(4)]) ;
FileName = '~/dummy' ;
unix(sprintf('rm %s.*',FileName)) ;
mlf2pdf(1,FileName,'amssymb')
Would be cool if someone have an idea.
Thanks, Ralf

답변 (1개)

the cyclist
the cyclist 2011년 8월 5일
This code will move the legend box to the right, by the same amount you trimmed the width. Will that work for you?
set(hleg,'position',[leg_pos(1)+0.2*leg_pos(3),leg_pos(2),...
leg_pos(3)*0.8,leg_pos(4)]) ;
The other strategy I thought of is very kludgy. You could
  • write the legend without the LaTeX
  • get the position of the legend
  • delete that legend
  • write the real legend
  • set the position of the real legend to the value you got
  댓글 수: 1
Ralf
Ralf 2011년 8월 5일
Thanks for your answer. But unfortunately the first solution is not working. I will try the second one later.
Anyway, thanks again.

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

카테고리

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