Clear Filters
Clear Filters

Size of legend box

53 views (last 30 days)
Ralf
Ralf on 5 Aug 2011
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

Answers (1)

the cyclist
the cyclist on 5 Aug 2011
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 Comment
Ralf
Ralf on 5 Aug 2011
Thanks for your answer. But unfortunately the first solution is not working. I will try the second one later.
Anyway, thanks again.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!