Fix bugs in legend positioning with loc='best' 

This PR fixes a couple of bugs which caused the legend positioning algorithm to ignore vertices when figuring out the best location of the legend box.