@@ -814,55 +814,31 @@ def _init_legend_box(self, handles, labels, markerfirst=True):
814
814
815
815
def _auto_legend_data (self ):
816
816
"""
817
- Returns list of vertices and extents covered by the plot .
817
+ Return display coordinates for hit testing for "best" positioning .
818
818
819
- Returns a two long list.
820
-
821
- First element is a list of (x, y) vertices (in
822
- display-coordinates) covered by all the lines and line
823
- collections, in the legend's handles.
824
-
825
- Second element is a list of bounding boxes for all the patches in
826
- the legend's handles .
819
+ Returns
820
+ -------
821
+ bboxes
822
+ List of bounding boxes of all patches.
823
+ lines
824
+ List of `.Path` corresponding to each line.
825
+ offsets
826
+ List of (x, y) offsets of all collection .
827
827
"""
828
- # should always hold because function is only called internally
829
- assert self .isaxes
830
-
828
+ assert self .isaxes # always holds, as this is only called internally
831
829
ax = self .parent
832
- bboxes = []
833
- lines = []
830
+ lines = [line .get_transform ().transform_path (line .get_path ())
831
+ for line in ax .lines ]
832
+ bboxes = [patch .get_bbox ().transformed (patch .get_data_transform ())
833
+ if isinstance (patch , Rectangle ) else
834
+ patch .get_path ().get_extents (patch .get_transform ())
835
+ for patch in ax .patches ]
834
836
offsets = []
835
-
836
- for handle in ax .lines :
837
- assert isinstance (handle , Line2D )
838
- path = handle .get_path ()
839
- trans = handle .get_transform ()
840
- tpath = trans .transform_path (path )
841
- lines .append (tpath )
842
-
843
- for handle in ax .patches :
844
- assert isinstance (handle , Patch )
845
-
846
- if isinstance (handle , Rectangle ):
847
- transform = handle .get_data_transform ()
848
- bboxes .append (handle .get_bbox ().transformed (transform ))
849
- else :
850
- transform = handle .get_transform ()
851
- bboxes .append (handle .get_path ().get_extents (transform ))
852
-
853
837
for handle in ax .collections :
854
- transform , transOffset , hoffsets , paths = handle ._prepare_points ()
855
-
856
- if len (hoffsets ):
857
- for offset in transOffset .transform (hoffsets ):
858
- offsets .append (offset )
859
-
860
- try :
861
- vertices = np .concatenate ([l .vertices for l in lines ])
862
- except ValueError :
863
- vertices = np .array ([])
864
-
865
- return [vertices , bboxes , lines , offsets ]
838
+ _ , transOffset , hoffsets , _ = handle ._prepare_points ()
839
+ for offset in transOffset .transform (hoffsets ):
840
+ offsets .append (offset )
841
+ return bboxes , lines , offsets
866
842
867
843
def get_children (self ):
868
844
# docstring inherited
@@ -1033,12 +1009,11 @@ def _find_best_position(self, width, height, renderer, consider=None):
1033
1009
*consider* is a list of ``(x, y)`` pairs to consider as a potential
1034
1010
lower-left corner of the legend. All are display coords.
1035
1011
"""
1036
- # should always hold because function is only called internally
1037
- assert self .isaxes
1012
+ assert self .isaxes # always holds, as this is only called internally
1038
1013
1039
1014
start_time = time .perf_counter ()
1040
1015
1041
- verts , bboxes , lines , offsets = self ._auto_legend_data ()
1016
+ bboxes , lines , offsets = self ._auto_legend_data ()
1042
1017
1043
1018
bbox = Bbox .from_bounds (0 , 0 , width , height )
1044
1019
if consider is None :
@@ -1051,10 +1026,10 @@ def _find_best_position(self, width, height, renderer, consider=None):
1051
1026
for idx , (l , b ) in enumerate (consider ):
1052
1027
legendBox = Bbox .from_bounds (l , b , width , height )
1053
1028
badness = 0
1054
- # XXX TODO: If markers are present, it would be good to
1055
- # take them into account when checking vertex overlaps in
1056
- # the next line.
1057
- badness = ( legendBox . count_contains ( verts )
1029
+ # XXX TODO: If markers are present, it would be good to take them
1030
+ # into account when checking vertex overlaps in the next line.
1031
+ badness = ( sum ( legendBox . count_contains ( line .vertices )
1032
+ for line in lines )
1058
1033
+ legendBox .count_contains (offsets )
1059
1034
+ legendBox .count_overlaps (bboxes )
1060
1035
+ sum (line .intersects_bbox (legendBox , filled = False )
0 commit comments