# legend outside the plot

Is there any way to display a legend in a second window or outside the plot?
Wolfgang

Here’s a legend function which allows you to specify loc=‘outer right’. The part for ‘outer left’ doesn’t work. It basically wrappes the pylab.legend method, it rescales the plot and sets the location of the legend to coordinates outside of the current axes.

def legend(*args, **kwargs):
“”"
Overwrites the pylab legend function.

``````It adds another location identfier 'outer right'
which locates the legend on the right side of the plot

The args and kwargs are forwarded to the pylab legend function
"""
if kwargs.has_key('loc'):
loc = kwargs['loc']
loc = loc.split()

if loc[0] == 'outer':
# make a legend with out the location
# remove the location setting from the kwargs
kwargs.pop('loc')
leg = pylab.legend
``````

(loc=(0,0), *args, **kwargs)
frame = leg.get_frame()
currentAxes = pylab.gca()
currentAxesPos = currentAxes.get_position()
# scale plot by the part which is taken by the legend

``````        plotScaling = frame.get_width()/currentAxesPos[2]

if loc[1] == 'right':
# scale the plot
currentAxes.set_position((currentAxesPos[0], currentAxesPos[1],

currentAxesPos[2] * (1-plotScaling),
currentAxesPos[3]))
# set x and y coordinates of legend
leg._loc = (1 + leg.axespad, 1 - frame.get_height())

# doesn't work
#if loc[1] == 'left':
#    # scale the plot
#    currentAxes.set_position((currentAxesPos[0] + frame.get_width(),
#                              currentAxesPos[1],
#                              currentAxesPos[2] * (1-plotScaling),
#                              currentAxesPos[3]))

#    # set x and y coordinates of legend
#    leg._loc = (1 -.05 -  leg.axespad - frame.get_width(), 1 - frame.get_height())

pylab.draw_if_interactive()
return leg

return pylab.legend(*args, **kwargs)
``````

Cheers! Bernhard

···

On 8/27/07, Wolfgang Kerzendorf < wkerzendorf@…982…> wrote:

Is there any way to display a legend in a second window or outside the plot?