Placing the legend outside the plot
Question: I want to position the legend outside the drawing box. I do not find a clean
way to do this. The main problem is having everything fit on the file saved.
The only thing I have been able to figure out is this code:

    
    
    #! /usr/bin/python
    import matplotlib
    # matplotlib.use('pdf')
    
    from matplotlib.pyplot import *
    subplot(111)
    plot([1,2,3], label="test1")
    l=legend(bbox_to_anchor=(1.05, 1), loc=2,borderaxespad=0)
    tight_layout(rect=(0,0,0.8,1))
    savefig('test.pdf')
    

There are a couple of caveats:

  1. The tight_layout seems to be incompatible with `matplotlib.use('pdf')`
  2. The 0.8 in the `tight_layout` has been found by trials and errors. If I replace `label="test"` by `label="this is a very very long test"`, the legend will once again get out of the border of the file. I would like that to be adjusted automatically. I have not been able to retrieve the size of the legend. `l.get_frame().get_width()` seems to always return 1.0.
  3. The `tight_layout` changes the size of the plot. What I would like to achieve is: specify the size of the plot in cm (or in inches). Placing the legend outside the plot. Having a file with the correct size in order that everything fits in, including the legend; without having to resort to trial and errors. 


Answer: You can look at the answer to
[this](http://stackoverflow.com/questions/4700614/how-to-put-the-legend-out-
of-the-plot) question by [Joe
Kington](http://stackoverflow.com/users/325565/joe-kington). The answer
describes most of the options you can play around with regarding placement of
legends.

