Size of legend box

53 次查看(过去 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