Multi-column legends

Hi folks,

I recently required a multi-column legend for a matplotlib graph. I
hacked up the Legend class to support this. I figured this might be
useful to others, so I'm attaching a patch in the hopes that someone
who is a regular contributor will review it and check it in.

Disclaimer: I spent an hour learning python for the express purpose of
making these graphs. That's the extent of my python experience so
some things might have been done the long way around.

It adds two parameters to the legend class:

The first 'rowspercolumn' specifies how often to start a new
column. If it is -1 then the current single-column behaviour is
reproduced. I set this to -1 in my matplotlibrc file, and pass a
specific number to the legend command when I want a multi-column

The second 'columnsep' specifies the distance between the right side
of one column's label and the left side of the line of the next

Columns are sized horizontally to fit the largest label in the
column. I've tested it "extensively" on my case, with various
combinations of columns and legend entries. It seems to be stable. But
"extensively" is probably not a huge amount, honestly, and I'm new to
matplotlib so I may not have covered all the cases.


p.s. the patch is against the MacOS X binary distro which is at

multi-line-legends.patch (9.21 KB)