This blog comes from an answer from How to put the legend out of the plot. Reprint it here for note. Original author keeps all the right.
A legend is positioned inside the bounding box of the axes using the
loc argument to
loc="upper right" places the legend in the upper right corner of the bounding box, which by default extents from
(1,1) in axes coordinates (or in bounding box notation
(x0,y0, width, height)=(0,0,1,1)).