Code covered by the BSD License  

Highlights from
legendflex: a more flexible legend

4.8

4.8 | 10 ratings Rate this file 60 Downloads (last 30 days) File Size: 10.11 KB File ID: #31092
image thumbnail

legendflex: a more flexible legend

by Kelly Kearney

 

18 Apr 2011 (Updated 24 Jun 2011)

Create a legend with more flexible positioning and labeling capabilities

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information
Description

With legendflex, you can:

- position the legend relative to any object in any figure, not just the parent axis of whatever is being labeled
 
- organize legend text and symbols in a grid with a specified number of rows and/or columns

- rescale the horizontal space used by each legend symbol
    
- create multiple legends for the same axis
    
- add a title to the legend within the legend box

MATLAB release MATLAB 7.10 (2010a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (17)
29 Apr 2011 Dan  
16 May 2011 Claudio

Well done! Very useful!

10 Jun 2011 Jason Kaeding

Another bug: if I delete the legend then resize my figure, I get an error...

??? Error using ==> getappdata
Invalid object handle

Error in ==> legendflex>resizefig at 621
    Lf = getappdata(ax(ia), 'legflex');
 
??? Error while evaluating figure ResizeFcn

10 Jun 2011 Jason Kaeding

I guess it didn't take my first bug submission...

The vertical positioning of the patches is incorrect in the following code:

figure;
area(1:10,rand(10,10))
legendflex(gca,cellstr(num2str(column(1:10))),'ncol',5,'anchor',[6 2],'buffer',[0 -10])

17 Jun 2011 Kelly Kearney

Vertical alignment bug has been corrected, and I added some checks to make sure the figure resize doesn't crash when you remove a legend. Thanks to Jason for discovering these bugs.

08 Aug 2011 Mario Liverpool

Very useful indeed!

10 Aug 2011 Philipp  
10 Aug 2011 Ben

Could you provide the example of getting the result as the screenshoot shows? I think the simple examples in the .m file is not fully demonstrate the capability of your function. Wish you could add more examples.

10 Jan 2012 Mukhtar Ullah

This function is the first serious fix to the long lasting problems Matlab has with legends.
The text interpreter is set none by this function and you could only change it when calling the function, and not later using SET. Is there a good reason for that?
Mukhtar

10 Jan 2012 Kelly Kearney

@Mukhtar Ullah,

I don't actually manually set the interpreter in this function, so it relies on the default. You can actually change this after the fact using set, as long as you return the object_h handles (see last syntax option); the first n indices of this array will correspond to the n text objects in the legend. This slightly obscure way of returning handles was chosen to be consistent with the original legend command.

14 Jan 2012 Mukhtar Ullah

Thank you Kelly for clarifying that. It occurred to me because with legend, it is possible to set properties such as interpreter, string,..using set directly on the axes handle.
A minor addition to legendflex will make it easier to find its handle using findobj and findall function. For example, if you add a tag 'legendflex' to the axes, findobj(gcf, 'tag', 'legendflex') would locate the axes.

Mukhtar

18 Jan 2012 Mukhtar Ullah

I just realized that it is possible to find the legendflex axes by
ax = getappdata(hfig, 'legflexchildren')
I now have another problem. When I use legendflex together with panel
http://www.mathworks.co.uk/matlabcentral/fileexchange/20003-panel
any attempt to resize the figure results in the following error
Error using panel.resizeCallback
Cannot access method 'resizeCallback' in class 'panel'.

Error in legendflex>resizefig (line 637)
                feval(Lf.oldrsz, hfig, ed);
 
Error using drawnow
Error while evaluating figure ResizeFcn

It turned out that commenting line 637 solves my problem.
Any idea of fixing this in a future release?

Thanks

19 Jan 2012 Kelly Kearney

@Mukhtar Ullah

Regarding making legendflex play nicely with panel, I admit I never considered the possibility that a figure's resize function might be linked to an object method. I don't do object-oriented coding myself, so I have very little knowledge of the internal workings, scope, etc. of objects. So no promises to fix this, though I will look into it if I find some spare time.

24 Jan 2012 Mukhtar Ullah

Thanks Kelly for your explanation. I am not sure what will I loose if I keep those lines commented out. At the moment, I am avoiding any figure resize when both legendflex and panel are at play.
 

07 Mar 2012 Camille Couzi

thanks a lot for this very useful code!!!

09 Apr 2012 Dominic  
27 Apr 2012 Chris

Very nice - thanks!
I wonder if anyone knows how to go a step beyond. That is, my legends are often like tables which I currently hand make as figures within figures. The table-like legend has row and column headings with only the symbols/line types in the cells instead of a label for every symbol/line. This is because the symbol itself might change to represent one variable and whether it's filled or open might represent another so it's identity is a combination of the row and column headings in the table.

Please login to add a comment or rating.
Updates
20 Apr 2011

Figure resize now re-links legend and labeled objects with updated line/patch properties.

16 Jun 2011

bug fixes: fixed error in function resize if you delete the legend, fixed minor vertical alignment issue

24 Jun 2011

Fixed bug where very wide titles were not completely enclosed in legend box.

Tag Activity for this File
Tag Applied By Date/Time
legend Kelly Kearney 19 Apr 2011 11:48:18
label Kelly Kearney 19 Apr 2011 11:48:18
potw Lindsay Coutinho 22 Jul 2011 10:36:54
pick of the week Lindsay Coutinho 22 Jul 2011 10:36:54

Contact us at files@mathworks.com