# legends

Is there a way for the legend to be outside of the

> axis? Perhaps rendered separately.

I added Figure.legend and the figlegend to the matlab interface. It
was as easy as I hoped.

In addition to the normal location codes (eg, 'upper center'), I
modified legend so you can specify the location as an x,y tuple in
relative coords. For axes 0,0 is the lower left of the axes and 1,1
is the upper right. Likewise for figure, but the left and top are for
the figure not the axes

There are two important differences from the axes legend:

* you must pass in a list of lines/rectangle, labels and a location
code. The figure legend doesn't try and guess the appropriate
legend.

* You can have as many figure legends as you want.

Here is a demo script:

from matplotlib.matlab import *

x1 = arange(0.0, 2.0, 0.1)
x2 = arange(0.0, 2.0, 0.01)

l1, l2 = plot(x1, sin(2*pi*x1), 'ro', x1, sin(4*pi*x1), 'g>')
l3, l4 = plot(x2, 0.5*sin(2*pi*x2), '-', x2, 2.0*sin(4*pi*x2), '--')

figlegend((l1, l2), ('one', 'two'), 'upper center')
l = figlegend((l3, l4), ('three', 'four'), 'lower left')
l.get_frame().set_facecolor('y')

figlegend((l1, l2), ('manual', 'placement'), loc=(0.6, 0.7))

show()

This function should be added to matlotlib.matlab.py:

def figlegend(handles, labels, loc):
"""
Place a legend in the figure. Labels are a sequence of
strings, handles is a sequence of line or patch instances, and
loc can be a string or an integer specifying the legend
location

USAGE:
legend( (line1, line2, line3),
('label1', 'label2', 'label3'),
'upper right')

See help(legend) for information about the location codes
"""
l= gcf().legend(handles, labels, loc)
draw_if_interactive()
return l

And the following attached files should be put in their proper places

legend.py (10.2 KB)

figure.py (4.47 KB)