File Exchange Pick of the Week

February 11th, 2011

Create Multi-Column Plot Legends

Brett's Pick this week is "columnlegend," by Simon Henin.

Today's Pick is another that is an author's first submission to the File Exchange. Simon shared a nice, easy-to-use function to create multi-column figure labels.

MATLAB's built-in legend command suffices for most cases when there are few objects to differentiate. However, when the number of lines plotted, for instance, gets large, columnlegend shines. It enables one essentially to reshape the default n-by-one legend into a p-by-q one.

Consider:

nLines = 18;
legend_str = cell(nLines,1);

myColors = distinguishable_colors(nLines);

(See my previous post about distinguishable_colors, or comment out the above line, and uncomment the following one.)

%myColors = jet(nLines);
t = 0:pi/64:3*pi;
dPhi = pi/16;
lineStyles = {'-', '--', ':', '-.'};
for ii=1:nLines,
    plot(t,sin(t+dPhi*ii),...
        'linestyle', lineStyles{rem(ii-1,numel(lineStyles))+1},...
        'color', myColors(ii,:),...
        'linewidth',3);
    hold on;
    legend_str{ii} = num2str(ii);
end
axis([0 3*pi -1.15 1.6])
legend(legend_str,'location','NorthWest')

Now consider how columnlegend improves the situation:

for ii=1:nLines,
    plot(t,sin(t+dPhi*ii),...
        'linestyle', lineStyles{rem(ii-1,numel(lineStyles))+1},...
        'color', myColors(ii,:),...
        'linewidth',3);
    hold on;
    legend_str{ii} = num2str(ii);
end
axis([0 3*pi -1.15 1.6])
columnlegend(6,legend_str,'NorthWest');

That's better! columnlegend also conveniently provides a handle to the legend-containing axes, allowing you to tweak parameters (like position). Very nice first effort, Simon!

One further note: in a comment on his submission page, Simon mentioned that he had fixed an issue with plot markers. As of this writing, that modification is still pending; it should be available shortly.

Let us know what you think, or leave a comment for Simon here.


Get the MATLAB code

Published with MATLAB® 7.11

3 Responses to “Create Multi-Column Plot Legends”

  1. Adrian Cherry replied on :

    Congraulations to Simon for the PotW award, this was a very useful submission and a great springboard for me to develop the code. However I feel it would be remiss of me at this point not to advertise a few updates I’ve included in my submission, gridLegend, which is linked from columnlegend.

    The changes I’ve made include :
    * enable legend properties to be included in the call

    gridLegend(hdl,8,gKey,'Orientation','Horizontal','Fontsize',8)

    * The orientation parameter name allows you to change whether the legend entries are column orientated or row orientated.

    * resizes the plot area so that the legend can be placed outside the axes.

    * retains the boxing around the legend, although ‘box’,'off’ still works.

    * works with clickableLegend, another useful PotW.

    * works with bar charts and errorbar plots.

    * I’ve fixed the printing problem in columnLegend, including a write up up in undocumented MATLAB http://undocumentedmatlab.com/blog/multi-column-grid-legend/
    so it should be possible to print directly or export to an image file.

    Regards

    Adrian

  2. Roman replied on :

    Thanks, for the submission. I am struggling with a pie chart which has too many line entries in the legend. I would like to break it into 2 columns in your spirit. Would this be possible?

    Kind regards

    Roman

  3. Bill replied on :

    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,%列位置,隔numpercolumn+1
    col = col+1;
    end
    else
    col=i-1;
    end

    to kill this bug.


MathWorks

Brett & Jiro share their favorite user-contributed submissions from the File Exchange.

These postings are the author's and don't necessarily represent the opinions of MathWorks.