I recently discovered matplotlib after there were some things that good old
gnuplot couldn't do, and quickly came to some of matplotlib's limitations.
But since it's written in python which is my favorite language :-) I hacked
together the attached patch.
It basically implements legend placement outside right of the axes. This means
three new options for the loc parameter in Legend's constructor: 'upper
outside right', 'center outside right' and 'lower outside right',
corresponding to codes 11-13. I didn't bother to have legends left, below or
above the plot.
One problem that I immediately came across was fitting the legend next to the
subplot. I thus made the legend resize the axes by default so that it fits
neatly next to it. This necessitates a small change in the drawing algorithm
in the Axes class as the axes need to be resized before they're drawn, and
the legend must be drawn *after* the axes. The whole thing works pretty well
in what I've tested so far, but can be turned off by using resize_axes=False,
a new keyword for Legend's constructor.
Further, the 'best' location now checks all possible inside locations and if
it doesn't find a perfect spot puts it 'center outside right' instead of at
the least problematic place.
Finally I cleaned up some redundant code in legend.py and two minor bugs, one
related to error checking of the loc parameter, the other to near-perfect
legend positioning in case it's frame uses a non-standard linewidth.
Bugs and caveats: Don't try to resize the axes when resize_axes is on. In my
scripts this seems to make no difference, but in gtk's interactive resizing
function this gives unexpected results.
Any chance of incorporating this enhancement in the next release?
Oh, and please bear with me, this is the first time I've contributed a
meaningful patch to an open source project.
Climate Systems Analysis Group
University of Cape Town
Republic of South Africa
Tel(w): ++27 21 650 2748