class_docstr: Place a legend on the figure/axes.
class_docstr_tok: ["Place","a","legend","on","the","figure\/axes","."]
docstr: Determine the best location to place the legend.
docstr_tok: ["Determine","the","best","location","to","place","the","legend","."]
code: def _find_best_position(self, width, height, renderer):
      """Determine the best location to place the legend."""
      assert (
          self.isaxes
      )  # always holds, as this is only called internally
  
      start_time = time.perf_counter()
  
      bboxes, lines, offsets = self._auto_legend_data()
  
      bbox = Bbox.from_bounds(0, 0, width, height)
  
      candidates = []
      for idx in range(1, len(self.codes)):
          l, b = self._get_anchored_bbox(
              idx, bbox, self.get_bbox_to_anchor(), renderer
          )
          legendBox = Bbox.from_bounds(l, b, width, height)
          # XXX TODO: If markers are present, it would be good to take them
          # into account when checking vertex overlaps in the next line.
          badness = (
              sum(
                  legendBox.count_contains(line.vertices)
                  for line in lines
              )
              + legendBox.count_contains(offsets)
              + legendBox.count_overlaps(bboxes)
              + sum(
                  line.intersects_bbox(legendBox, filled=False)
                  for line in lines
              )
          )
          # Include the index to favor lower codes in case of a tie.
          candidates.append((badness, idx, (l, b)))
          if badness == 0:
              break
  
      _, _, (l, b) = min(candidates)
  
      if (
          self._loc_used_default
          and time.perf_counter() - start_time > 1
      ):
          _api.warn_external(
              'Creating legend with loc="best" can be slow with large '
              "amounts of data."
          )
  
      return l, b
  
imports: import itertools
  import logging
  import numbers
  import time
  import numpy
  import matplotlib
  from matplotlib import _api, _docstring, cbook, colors, offsetbox
  from matplotlib.artist import Artist, allow_rasterization
  from matplotlib.cbook import silent_list
  from matplotlib.font_manager import FontProperties
  from matplotlib.lines import Line2D
  from matplotlib.patches import Patch, Rectangle, Shadow, FancyBboxPatch, StepPatch
  from matplotlib.collections import Collection, CircleCollection, LineCollection, PathCollection, PolyCollection, RegularPolyCollection
  from matplotlib.text import Text
  from matplotlib.transforms import Bbox, BboxBase, TransformedBbox
  from matplotlib.transforms import BboxTransformTo, BboxTransformFrom
  from matplotlib.offsetbox import AnchoredOffsetbox, DraggableOffsetBox, HPacker, VPacker, DrawingArea, TextArea
  from matplotlib.container import ErrorbarContainer, BarContainer, StemContainer
  from .None import legend_handler
label_desc: Use image node_id 33 for calling the Legend obj's underlying member method code with example usage: obj._find_best_position(width, height, renderer) and returns: l,  b