Code covered by the BSD License  

Highlights from
columnlegend

5.0

5.0 | 6 ratings Rate this file 235 Downloads (last 30 days) File Size: 4.82 KB File ID: #27389
image thumbnail

columnlegend

by Simon Henin

 

28 Apr 2010 (Updated 14 Apr 2013)

Creates a legend with a specified number of columns

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information
Description

UPDATED 04/09/2013.
Thanks to comments for bug fixes/improvements.

Creates a legend with a specified number of columns

usage:

       legend_str = [];
       for i=1:10,
            x = 1:i:(10*i);
            plot(x); hold on;
            legend_str = [legend_str; {num2str(i)}];
       end
       columnlegend(3, legend_str, 'Location', 'NorthWest', 'boxon');
         

PRINTING: If you want to print the figure, you'll have to use a command line printer (e.g. export_fig) as the File->Print option embedded in the figure resets the figure properties including the legend

Acknowledgements

This file inspired Grid Legend A Multi Column Format For Legends.

MATLAB release MATLAB 7.7 (R2008b)
Tags for This File  
Everyone's Tags
axis(2), columns(3), figure(2), graph, legend, legends(2), line, pick of the week, potw
Tags I've Applied
Add New Tags Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (17)
30 Apr 2013 Thomine Stolberg-Rohr  
10 Apr 2013 Ligong Han

very useful

27 Sep 2012 Mai

I want to change the fontsize of the legend to 14, is that possible?
The text is to small to see in a document, the way it is.

13 Jun 2012 Bill

I found a bug of this program. For example, when you try to change 1_by_2 legends to 2_by_1, this program will not work. Because the numpercolumn is 1, and the col is always -1.

Replace the code

if mod(i,numpercolumn)==1,
col = col+1;
end

with

if numpercolumn>1,
if mod(i,numpercolumn)==1,
col = col+1;
end
else
col=i-1;
end

to kill this bug.

13 Jun 2012 Bill  
20 May 2012 Durga Lal Shrestha

The columnlegend hides the box around the legend as it is not possible to force the box to be smaller than the
original height. The work around to display box is to use annotation.

The following code can be added at the very end of "columnlegend" to show the box around legend:

pos = get(legend_h, 'position');
orgHeight =pos(4);
pos(4) = orgHeight/numlines*numpercolumn;
pos(2)=pos(2)+ orgHeight-pos(4);
annotation('rectangle',pos)

16 Mar 2012 Javier Pacheco Labrador

Hi Simon,

I mixed line plots and scatter plots, what a pitty.

Anyhow, the function is great.

Thanks.

Javier

16 Mar 2012 Simon Henin

@ Javier

Unfortunately, at this time this script only works for line graphs, such as created via the plot command. You were probably trying to use the script on a bar or other non-typical matlab figure object.

Best,
Simon

16 Mar 2012 Javier Pacheco Labrador

Hi,

I am having a problem with one of my graphs:

??? Error using ==> get
There is no 'xdata' property in the 'hggroup' class.

Error in ==> columnlegend at 42
xdata = get(object_h(numlines+1), 'xdata');

I dunno the reason. The function works with the example on http://blogs.mathworks.com/pick/2011/02/11/create-multi-column-plot-legends/

06 Mar 2012 Sturla Kvamsdal

Update: The problems probably has nothing to do with subplots (tried it in a simple example). Error message:
??? Index exceeds matrix dimensions.

Error in ==> columnlegend at 77
set(object_h(linenum), 'ydata', [(height-(position-1)*sheight)
(height-(position-1)*sheight)]);

06 Mar 2012 Sturla Kvamsdal

Brilliant, just what I need. Have problems with subplots, though.

06 Dec 2011 Simon Henin

@Mathias

Please refer to the full file description. To export to vector graphics files, use an advanced figure export (I recommend export_fig, http://www.mathworks.com/matlabcentral/fileexchange/23629).

06 Dec 2011 Mathias Vanwolleghem

not compatible with eps format.
So not useful to generate figures to be included in latex documents....

05 Sep 2011 Aslak Grinsted

Nice, useful and compact code! It would be nice if i could give it a list of handles to put in the legend.

08 Feb 2011 Simon Henin

Thanks for the comment, this has been fixed.

08 Feb 2011 Amy

Nice, but doesn't work if you have used markers.

06 Jul 2010 Martyn Dorey

Genius

Updates
30 Apr 2010

Removed Mac OS hidden files from zip file.

11 May 2010

Add instructions for printing figure with columns

08 Feb 2011

Added functionality when using markers.

10 Feb 2011

Minor bug fix

28 Feb 2011

removed hidden files.

09 Apr 2013

Updates/Bug Fixes

14 Apr 2013

Bug fix

Contact us