@@ -824,55 +824,34 @@ def _init_legend_box(self, handles, labels, markerfirst=True):
824
824
825
825
def _auto_legend_data (self ):
826
826
"""
827
- Returns list of vertices and extents covered by the plot .
827
+ Return display coordinates for hit testing for "best" positioning .
8000
span>
828
828
829
- Returns a two long list.
830
-
831
- First element is a list of (x, y) vertices (in
832
- display-coordinates) covered by all the lines and line
833
- collections, in the legend's handles.
834
-
835
- Second element is a list of bounding boxes for all the patches in
836
- the legend's handles.
829
+ Returns
830
+ -------
831
+ vertices
832
+ List of (x, y) vertices of all lines.
833
+ bboxes
834
+ List of bounding boxes of all patches.
835
+ lines
836
+ List of `.Path` corresponding to each line.
837
+ offsets
838
+ List of (x, y) offsets of all collection.
837
839
"""
838
- # should always hold because function is only called internally
839
- assert self .isaxes
840
-
840
+ assert self .isaxes # always holds, as this is only called internally
841
841
ax = self .parent
842
- bboxes = []
843
- lines = []
842
+ lines = [line .get_transform ().transform_path (line .get_path ())
843
+ for line in ax .lines ]
844
+ vertices = np .concatenate ([l .vertices for l in lines ]) if lines else []
845
+ bboxes = [patch .get_bbox ().transformed (patch .get_data_transform ())
846
+ if isinstance (patch , Rectangle ) else
847
+ patch .get_path ().get_extents (patch .get_transform ())
848
+ for patch in ax .patches ]
844
849
offsets = []
845
-
846
- for handle in ax .lines :
847
- assert isinstance (handle , Line2D )
848
- path = handle .get_path ()
849
- trans = handle .get_transform ()
850
- tpath = trans .transform_path (path )
851
- lines .append (tpath )
852
-
853
- for handle in ax .patches :
854
- assert isinstance (handle , Patch )
855
-
856
- if isinstance (handle , Rectangle ):
857
- transform = handle .get_data_transform ()
858
- bboxes .append (handle .get_bbox ().transformed (transform ))
859
- else :
860
- transform = handle .get_transform ()
861
- bboxes .append (handle .get_path ().get_extents (transform ))
862
-
863
850
for handle in ax .collections :
864
851
transform , transOffset , hoffsets , paths = handle ._prepare_points ()
865
-
866
- if len (hoffsets ):
867
- for offset in transOffset .transform (hoffsets ):
868
- offsets .append (offset )
869
-
870
- try :
871
- vertices = np .concatenate ([l .vertices for l in lines ])
872
- except ValueError :
873
- vertices = np .array ([])
874
-
875
- return [vertices , bboxes , lines , offsets ]
852
+ for offset in transOffset .transform (hoffsets ):
853
+ offsets .append (offset )
854
+ return (vertices , bboxes , lines , offsets )
876
855
877
856
def draw_frame (self , b ):
878
857
"""
@@ -1056,8 +1035,7 @@ def _find_best_position(self, width, height, renderer, consider=None):
1056
1035
*consider* is a list of ``(x, y)`` pairs to consider as a potential
1057
1036
lower-left corner of the legend. All are display coords.
1058
1037
"""
1059
- # should always hold because function is only called internally
1060
- assert self .isaxes
1038
+ assert self .isaxes # always holds, as this is only called internally
1061
1039
1062
1040
start_time = time .perf_counter ()
1063
1041
@@ -1074,9 +1052,8 @@ def _find_best_position(self, width, height, renderer, consider=None):
1074
1052
for idx , (l , b ) in enumerate (consider ):
1075
1053
legendBox = Bbox .from_bounds (l , b , width , height )
1076
1054
badness = 0
1077
- # XXX TODO: If markers are present, it would be good to
1078
- # take them into account when checking vertex overlaps in
5942
tr>1079
- # the next line.
1055
+ # XXX TODO: If markers are present, it would be good to take them
1056
+ # into account when checking vertex overlaps in the next line.
1080
1057
badness = (legendBox .count_contains (verts )
1081
1058
+ legendBox .count_contains (offsets )
1082
1059
+ legendBox .count_overlaps (bboxes )
0 commit comments