Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Reduce legend size

4,230 views
Skip to first unread message

Daniel Wesemeier

unread,
Oct 20, 2008, 4:37:01 PM10/20/08
to
Hallo,

I intend to produce Matlab figures which are about 8cm wide on a page generated with LaTeX. After resizing the figure the legend appears quite big and the legend covers a lot of my plot. The font size can easily be changed by the "FontSize" property. And I found a thread which shows how the sample lines can be reduced in length. (http://www.mathworks.com/matlabcentral/newsreader/view_thread/155063#389116)

Here is the Problem: After I have resized the legend sample line length I tried to reduce the size of the legend box via the legend's "Position" property. But the size can only be increased but not decreased.

Is there a way to decrease the size of the legend box?

Thanks!

Nicolaie Popescu-Bodorin

unread,
Oct 20, 2008, 5:46:02 PM10/20/08
to
Hello there,

take the black cursor from the toolbar.
left-click on the legend corner
right-click and select 'unlock axex position' or something like this ...

Bodorin

"Daniel Wesemeier" <d.wes...@gmx.de> wrote in message <gdiq5d$t5q$1...@fred.mathworks.com>...

NZTideMan

unread,
Oct 20, 2008, 5:39:13 PM10/20/08
to
On Oct 21, 9:37 am, "Daniel Wesemeier" <d.weseme...@gmx.de> wrote:
> Hallo,
>
> I intend to produce Matlab figures which are about 8cm wide on a page generated with LaTeX. After resizing the figure the legend appears quite big and the legend covers a lot of my plot. The font size can easily be changed by the "FontSize" property. And I found a thread which shows how the sample lines can be reduced in length. (http://www.mathworks.com/matlabcentral/newsreader/view_thread/155063#...)

>
> Here is the Problem: After I have resized the legend sample line length I tried to reduce the size of the legend box via the legend's "Position" property. But the size can only be increased but not decreased.
>
> Is there a way to decrease the size of the legend box?
>
> Thanks!

You'll have to post how exactly you used 'Position' to resize the
legend box. I do this all the time and have no problems, so in
principle what you are doing is correct, but the actual implementation
may be wrong.

Daniel Wesemeier

unread,
Jan 24, 2010, 7:55:05 AM1/24/10
to
Hi there,

Thank you for your answers. Somehow I missed the notification. And I found a work around. I resized the Matlab figure twice the size I required and scaled it by half via LaTeX. So I forgot to come back to this thread. But now it turns out this work around has a little troubles when the figure is resized in LaTeX again.

@NZTideMan
I change the Position via get(...,'Position') and set(...,'Position',...).
here is an example:
AllObjects = findobj(gcf);
% Find the legend objects
alllegends = findall(AllObjects,'Tag','legend')
% Identify the legend content
childs = get(alllegends(1),'Children')
n_legent = length(childs);
allmarkers = childs(1:3:n_legent)
alllines = childs(2:3:n_legent)
alltext = childs(3:3:n_legent)
% Readjusting the linest
for it = 1:length(alllines)
xcor = get(alllines(it),'xdata')
leftshift = (xcor(2)-xcor(1))/2;
set(alllines(it),'xdata',[xcor(1) xcor(1)+(xcor(2)-xcor(1))/2]);
end
% Readjusting the text
for it = 1:length(alltext)
pos = get(alltext(it),'Position');
set(alltext(it),'Position',[pos(1)-leftshift pos(2)]);
end
% Change the Position Property
posleg = get(alllegends(1),'Position')
set(alllegends(1),'Position',[posleg(1) posleg(2) posleg(3)-leftshift posleg(4)]);

@Nicolaie Popescu-Bodorin
There is an "Edit Plot" tool and a "Data Cursor" tool. But neither has an "unlock axes option" in its content menu.

Has anyone another hint? Is the a hidden property somewhere that prevent the legend from be resized to small?

(By the way: I am still using Matlab 2007b. Maybe that is the problem.)

daniel

unread,
Mar 13, 2010, 11:41:05 AM3/13/10
to
I am having same problem in matlab 2009a. legend size can be increased but it refuses to decrease. the best workaround I can think of is to set(lh, 'box', 'off') and then draw my own box.

Jan Sousek

unread,
Mar 19, 2010, 5:21:05 AM3/19/10
to
I'm facing this problem as well.

us

unread,
Mar 19, 2010, 5:46:03 AM3/19/10
to
"Jan Sousek" <jan.s...@unibw.de> wrote in message <hnvfm0$noq$1...@fred.mathworks.com>...

> I'm facing this problem as well.

one of the (tedious) solutions

x=0:360;
y=sort(rand(size(x)));
line(x,y);
lh=legend('a');
disp('press any key to make legend smaller...');
pause;
set(lh,'units','pixels');
lp=get(lh,'outerposition');
set(lh,'outerposition',[lp(1:2),50,15]);

us

Jan Sousek

unread,
Mar 19, 2010, 6:43:10 AM3/19/10
to
"us " <u...@neurol.unizh.ch> wrote in message <hnvh4r$g17$1...@fred.mathworks.com>...

work's! Very nice. So the trick is to change the 'units'. Thanks
Jan.

Saras Saraswathi

unread,
Apr 26, 2012, 1:46:17 AM4/26/12
to
Yes, works well. But if you use the figure ( saved as png or other formats) and use it in latex document the small box moves to the center and creates havoc ! It does not stay with the rest of the picture.

saras




"Jan Sousek" wrote in message <hnvkfu$65c$1...@fred.mathworks.com>...

Bibi

unread,
May 11, 2012, 8:55:13 AM5/11/12
to
"Jan Sousek" wrote in message <hnvfm0$noq$1...@fred.mathworks.com>...

Anthony Lmaur

unread,
Aug 13, 2014, 10:32:08 AM8/13/14
to
"Bibi " <uce...@ucl.ac.uk> wrote in message <joj27h$s5o$1...@newscl01ah.mathworks.com>...
> "Jan Sousek" wrote in message <hnvfm0$noq$1...@fred.mathworks.com>...
> > I'm facing this problem as well.


Hi,

Another trick is to change the plot to box aspect ratio.

legend('string','Plotboxaspectratio',[0.8 0.8 0.8])

The numbers in the brackets are x y and z aspect ratio between the plot and the legend box
This way you will decrease to 80% the size of the legend box.
The thing is that you might need to change the font size as well to make it fit within the box. To do so, you can just add 'Fontsize',size_number within the legend command.

Hope this helps,

Anthony

Xiangping

unread,
Aug 14, 2014, 8:46:11 AM8/14/14
to
"us" wrote in message <hnvh4r$g17$1...@fred.mathworks.com>...
Many thanks. I have solved my problem by your method.
0 new messages