I came across matplotlib code which customizes legend location using keywords loc and bbox_to_anchor. For example :
fi
The explanation of @Gabriel is slightly misleading. bbox_to_anchor=[x0, y0] will create a bounding box with lower left corner at position [x0, y0]. The extend of the bounding box is zero - being equivalent to bbox_to_anchor=[x0, y0, 0, 0]. The legend will then be placed 'inside' this box and overlapp it according to the specified loc parameter. So loc specifies where inside the box the legend sits.
Also see this question What does a 4-element tuple argument for 'bbox_to_anchor' mean in matplotlib?