
@wOGc           @   si  d  Z  d d k l Z d d k Z d d k Z d d k Z d d k l Z l	 Z	 d d k
 l Z d d k l Z l Z l Z l Z d d k l Z d d k l Z d d	 k l Z d d
 k l Z l Z l Z l Z l Z l Z d d k l Z l  Z  l! Z! d d k" l# Z# d d k$ l% Z% l& Z& l' Z' l( Z( l) Z) l* Z* l+ Z+ l, Z, d   Z- d e f d     YZ. d S(   s  
Place a legend on the axes at location loc.  Labels are a
sequence of strings and loc can be a string or an integer
specifying the legend location

The location codes are

  'best'         : 0, (only implemented for axis legends)
  'upper right'  : 1,
  'upper left'   : 2,
  'lower left'   : 3,
  'lower right'  : 4,
  'right'        : 5,
  'center left'  : 6,
  'center right' : 7,
  'lower center' : 8,
  'upper center' : 9,
  'center'       : 10,

Return value is a sequence of text, line instances that make
up the legend
i(   t   divisionN(   t   verboset   rcParams(   t   Artist(   t	   enumeratet   is_string_liket   iterablet   silent_list(   t   FontProperties(   t   Line2D(   t   segments_intersect(   t   Patcht	   Rectanglet   RegularPolygont   Shadowt   bbox_artistt	   draw_bbox(   t   LineCollectiont   RegularPolyCollectiont   PatchCollection(   t   Text(   t   Bboxt   Pointt   Valuet   get_bbox_transformt   bbox_allt	   unit_bboxt   inverse_transform_bboxt   lbwh_to_bboxc         C   s0  | i    \ } } } } | | } | | } t |   } | d j o t Sn | d j o$ | i |  d d |  d d  Sn |  d }	 x |  d D] }
 |	 |
 f } xz | | f | | f f | | f | | f f | | f | | f f | | f | | f f f D] } t | |  o t Sq q W|
 }	 q Wt S(   s,    Return True if and only if line cuts bbox. i    i   (   t
   get_boundst   lent   Falset   containsR
   t   True(   t   linet   bboxt   minxt   minyt   widtht   heightt   maxxt   maxyt   nt   p1t   p2t   segmentt   edge(    (    s:   /Users/cmoad/workspace/matplotlib/lib/matplotlib/legend.pyt   line_cuts_bbox+   s*    

$
  
t   Legendc           B   sI  e  Z d  Z h  d d <d d <d d <d d <d	 d
 <d d <d d <d d <d d <d d <d d <Z d Z d   Z d* d* d* d* d* d* d* d* d* d* d 
 Z d   Z d   Z	 d   Z
 d   Z d   Z d   Z d   Z d    Z d!   Z d"   Z d#   Z d$   Z d%   Z d&   Z d* d'  Z d(   Z d)   Z RS(+   sX  
    Place a legend on the axes at location loc.  Labels are a
    sequence of strings and loc can be a string or an integer
    specifying the legend location

    The location codes are

      'best'         : 0, (only implemented for axis legends)
      'upper right'  : 1,
      'upper left'   : 2,
      'lower left'   : 3,
      'lower right'  : 4,
      'right'        : 5,
      'center left'  : 6,
      'center right' : 7,
      'lower center' : 8,
      'upper center' : 9,
      'center'       : 10,

    Return value is a sequence of text, line instances that make
    up the legend
    i    t   besti   s   upper righti   s
   upper lefti   s
   lower lefti   s   lower righti   t   righti   s   center lefti   s   center righti   s   lower centeri	   s   upper centeri
   t   centerc         C   s   d S(   NR0   (    (   t   self(    (    s:   /Users/cmoad/workspace/matplotlib/lib/matplotlib/legend.pyt   __str__o   s    c         C   s  d d k  l } d d k l } t i |   | | | |	 |
 | | | g } d d d d d d	 d
 d g } xL t | |  D]; \ } } | d" j o t d | } n t	 |  | |  qy W| d" j o t
 d t d  |  _ n
 | |  _ |  i i   |  _ t | |  o t |  _ |  i | i  n7 t | |  o t |  _ |  i |  n t d   | |  _ |  i t t   | i   | d" j o0 t d } |  i o | d# j o
 d } qn t |  o |  i i |  pp |  i o3 t i d | d i |  i i    f  d } q\t i d | d i |  i i    f  d } q`|  i | } n |  i o$ | d j o t i d  d } n | |  _  d$ \ } } |  i! d j o$ t" i# | |  i$ d g  |  _% n# t" i& | | |  i$ |  i!  |  _% | |  i$ |  i' } |  i( | | |  |  _) |  i* | |  i)  |  _+ t, |  i)  o6 |  i) d i-   \ } } |  i.   t, |  i)  } n d } | | } | |  i$ |  i' |  i/ 8} t0 d | | f d d d | d d d  d!  |  _1 |  i2 |  i1  t |  _3 d" S(%   s  
  parent                # the artist that contains the legend
  handles               # a list of artists (lines, patches) to add to the legend
  labels                # a list of strings to label the legend
  loc                   # a location code
  numpoints = 4         # the number of points in the legend line
  prop = FontProperties(size='smaller')  # the font property
  pad = 0.2             # the fractional whitespace inside the legend border
  markerscale = 0.6     # the relative size of legend markers vs. original
  shadow                # if True, draw a shadow behind legend

The following dimensions are in axes coords
  labelsep = 0.005     # the vertical space between the legend entries
  handlelen = 0.05     # the length of the legend lines
  handletextsep = 0.02 # the space between the legend line and legend text
  axespad = 0.02       # the border between the axes and legend edge
        i(   t   Axes(   t   Figuret	   numpointst   padt   markerscalet   labelsept	   handlelent   handletextsept   axespadt   shadows   legend.t   sizes   legend.fontsizes,   Legend needs either Axes or Figure as parents
   legend.loci    R1   s   upper rightsL   Unrecognized location "%s". Falling back on "best"; valid locations are
	%s
s   
	sS   Unrecognized location "%s". Falling back on "upper right"; valid locations are
	%s
i   si   Automatic legend placement (loc="best") not implemented for figure legend. Falling back on "upper right".g      ?g?t   xyR&   R'   t	   facecolort   wt	   edgecolort   kN(   i    s   best(   g      ?g      ?(4   t   axesR6   t   figureR7   R   t   __init__t   zipt   NoneR   t   setattrR   t   propt   get_size_in_pointst   fontsizet
   isinstanceR!   t   isaxest
   set_figureR   t	   TypeErrort   parentt   set_transformR   R   R#   R   t   codest   has_keyt   warningst   warnt   joint   keyst   _locR8   t   npyt   arrayR<   t   _xdatat   linspaceR=   t
   _get_textst   textst   _get_handlest   legendHandlesR   t   get_positiont   _approx_text_heightR9   R   t   legendPatcht   _set_artist_propst
   _drawFrame(   R4   RS   t   handlest   labelst   locR8   RL   R9   R:   R;   R<   R=   R>   R?   R6   R7   t   proplistt	   propnamest   namet   valuet   leftt   topt   textleftt   HEIGHTt   bottom(    (    s:   /Users/cmoad/workspace/matplotlib/lib/matplotlib/legend.pyRH   r   sv     				

	 
	 

	$"
c         C   s'   | i  |  i  | i |  i    d  S(   N(   RQ   RG   RT   t   get_transform(   R4   t   a(    (    s:   /Users/cmoad/workspace/matplotlib/lib/matplotlib/legend.pyRg      s    c         C   s+   |  i  d |  i i i   |  i i i   S(   Ng      R@(   RN   RG   t   dpit   getRS   R#   R'   (   R4   (    (    s:   /Users/cmoad/workspace/matplotlib/lib/matplotlib/legend.pyRe      s    c         C   s  |  i    p d  Sn | i d  |  i |  |  i oD |  i o& t |  i d d  } | i |  n |  i i |  n t |  i	  o t |  i
  o d  Sn x/ |  i	 D]$ } | d  j	 o | i |  q q Wx |  i
 D] } | i |  q W| i d  d  S(   Nt   legendg{Gzt(   t   get_visiblet
   open_groupt   _update_positionsRh   R?   R   Rf   t   drawR   Rc   Ra   RJ   t   close_group(   R4   t   rendererR?   t   ht   t(    (    s:   /Users/cmoad/workspace/matplotlib/lib/matplotlib/legend.pyR}      s$    

*
 
 c         C   s   g  } |  i  D] } | | i |  q ~ } g  } |  i D]' } | d j	 o | | i |  q; q; ~ } | } | i |  t |  }	 |	 |  _ t |  i   |	  }
 |
 |  _	 |
 S(   s0   Get a bbox for the text and lines in axes coordsN(
   Ra   t   get_window_extentRc   RJ   t   extendR   t   saveR   Ru   t   ibox(   R4   R   t   _[1]R   t
   bboxesTextt   _[2]R   t   bboxesHandlest	   bboxesAllR#   R   (    (    s:   /Users/cmoad/workspace/matplotlib/lib/matplotlib/legend.pyt   _get_handle_text_bbox  s    ->		c      	   C   s  |  i    } g  } xt | |  D]\ } } | i   \ } } | |  i |  i 8} t | t  o | | d t i |  i	 i
 t  }	 t |  i	 |	  }
 |
 i |  |  i |
  |
 i d   |
 i |  i |
 i    | i |
  q" t | t  oq t d t |  i	  | d	 | f d |  i d | d  } | i |  |  i |  | i d   | i |  q" t | t  o | | d t i |  i	 i
 t  }	 t |  i	 |	  }
 |  i |
  |
 i d   | i   d } | i   } | i   d } |
 i |  |
 i |  |
 i |  | i |
  q" t | t  o t d t |  i	  | d
 | f d |  i d | d  } | i | i  d  | i! d j o | i" | i! d  n |  i |  | i d   | i |  q" | i d   q" W| S(   Ni   RA   i   i   R&   R'   i    RJ   g      ?g      ?(#   Re   RI   Rd   R<   R=   RO   R	   R\   t   onesR^   t   shapet   floatt   update_fromRg   t   set_clip_boxRJ   t   set_markersizeR:   t   get_markersizet   appendR   R   t   minR   t   get_linewidtht
   get_dashest
   get_colorst	   set_colort   set_linewidtht
   set_dashesR   t   set_facecolort   _facecolorst   _edgecolorst   set_edgecolor(   R4   Ri   Ra   Rs   t   rett   handlet   labelt   xt   yt   ydatat   leglinet   pt   lwt   dashest   color(    (    s:   /Users/cmoad/workspace/matplotlib/lib/matplotlib/legend.pyRb     sX     $#$#c         C   s(  |  i  p t  |  i } g  } g  } g  } | i i } x | i D] } t | t  p t  | i d t	  } | i
 d t	  } | i   }	 |	 i | |  \ }
 } g  } t |
 |  D] } | | |  q ~ } | i |  qB Wx | i D] } t | t  p t  | i   } | i   }	 |	 i |  } g  } | D] } | | |  qA~ } t   } | i | t  | i |  q Wx | i D] } t | t  op | i   } | i   }	 xU | D]I } |	 i |  } g  } | D] } | | |  q~ } | i |  qWqqW| | | g S(   sp   Returns list of vertices and extents covered by the plot.

        Returns a two long list.

        First element is a list of (x, y) vertices (in
        axes-coordinates) covered by all the lines and line
        collections, in the legend's handles.

        Second element is a list of bounding boxes for all the patches in
        the legend's handles.
        t   orig(   RP   t   AssertionErrorRS   t	   transAxest   inverse_xy_tupt   linesRO   R	   t	   get_xdataR   t	   get_ydataRu   t   numerix_x_yRI   R   t   patchesR   t	   get_vertst   seq_xy_tupsR   t   updateR!   t   collectionsR   t	   get_lines(   R4   t   axt   verticest   bboxesR   t   invR   t   xdataR   t   transt   xtt   ytR   t   vt   avertst   vertst   tvertsR   R#   t   hlinesR"   t   tlinet   _[3]t   aline(    (    s:   /Users/cmoad/workspace/matplotlib/lib/matplotlib/legend.pyt   _auto_legend_dataI  sH    	
 0
 '	
  'c         C   s   | |  _  d S(   s$   b is a boolean.  Set draw frame to bN(   Rh   (   R4   t   b(    (    s:   /Users/cmoad/workspace/matplotlib/lib/matplotlib/legend.pyt
   draw_frame  s    c         C   s   |  i  S(   s6   return the Rectangle instance used to frame the legend(   Rf   (   R4   (    (    s:   /Users/cmoad/workspace/matplotlib/lib/matplotlib/legend.pyt	   get_frame  s    c         C   s6   g  } |  i  D]! } t | t  o | | q q ~ S(   s5   return a list of lines.Line2D instances in the legend(   Rc   RO   R	   (   R4   R   R   (    (    s:   /Users/cmoad/workspace/matplotlib/lib/matplotlib/legend.pyR     s    c         C   s?   t  d g  } |  i D]! } t | t  o | | q q ~  S(   s.   return a list of patch instances in the legendR   (   R   Rc   RO   R   (   R4   R   R   (    (    s:   /Users/cmoad/workspace/matplotlib/lib/matplotlib/legend.pyt   get_patches  s    c         C   s   t  d |  i  S(   s1   return a list of text.Text instance in the legendR   (   R   Ra   (   R4   (    (    s:   /Users/cmoad/workspace/matplotlib/lib/matplotlib/legend.pyt	   get_texts  s    c   
      C   s   |  i    } | } | } g  } xb | D]Z } t d | d | d | d |  i d d d d  }	 |  i |	  | i |	  | | 8} q% W| S(	   NR   R   t   textt   fontpropertiest   verticalalignmentRq   t   horizontalalignmentRp   (   Re   R   RL   Rg   R   (
   R4   Rj   Rp   t   upperRs   t   posR   R   t   lR   (    (    s:   /Users/cmoad/workspace/matplotlib/lib/matplotlib/legend.pyR`     s      		c         C   s   |  i  i   S(   N(   Rf   R   (   R4   (    (    s:   /Users/cmoad/workspace/matplotlib/lib/matplotlib/legend.pyR     s    c      	   C   s  x> |  i  D]3 } | i   \ } } | i | | | | f  q
 Wx |  i D] } t | t  oA | i d t  | i d t  } } | i	 | | | |  qK t | t
  o4 | i d | | i d <| i d | | i d <qK t | t  o? g  } | i D]! \ } } | | | | | f q~ | _ qK qK W|  i i   |  i i   } } |  i i | |  |  i i | |  d S(   s+   Move all the artists by ox,oy (axes coords)R   i    i   N(   Ra   Rd   t   set_positionRc   RO   R	   R   R   R   t   set_dataR   RA   R   R   Rf   t   get_xt   get_yt   set_xt   set_y(   R4   t   oxt   oyR   R   R   R   R   (    (    s:   /Users/cmoad/workspace/matplotlib/lib/matplotlib/legend.pyt   _offset  s"    
 
 %Cc         C   s  |  i  p t  |  i   \ } } } g  } t d t |  i   D] } | |  i | | |  qC ~ } |  i i \ }	 }
 g  } x | D] \ } } t	 | | | |  } d } | i
 |  } | |	 | |
 } } x, | D]$ } | i |  o | d 7} q q Wx, | D]$ } t | |  o | d 7} qqW| d j o | | f Sn | i | | | f f  q W| d } x- | D]% } | d | d j  o
 | } qyqyW| d \ } } | | f S(   s   Determine the best location to place the legend.

        `consider` is a list of (x, y) pairs to consider as a potential
        lower-left corner of the legend. All are axes coords.
        i   i    (   RP   R   R   t   rangeR   RU   t   _loc_to_axes_coordsRf   RA   R   t   count_containst   overlapsR/   R   (   R4   R&   R'   t   considerR   R   R   R   R   t   txt   tyt
   candidatesR   R   t	   legendBoxt   badnessR   R   R#   R"   t   minCandidatet	   candidate(    (    s:   /Users/cmoad/workspace/matplotlib/lib/matplotlib/legend.pyt   _find_best_position  s:    B   
 c         C   sF  | t  d d  j p t  t  d  \ } } } } } }	 }
 } } } } | | | |
 f j o |  i } nW | | | | |	 f j o d | |  i } n) | | | | f j o d | d } n | | | | f j o d | |  i } nO | | | | f j o |  i } n, | |
 | | |	 f j o d | d } n | | f S(   s   Convert a location code to axes coordinates.

        - loc: a location code in range(1, 11).
          This corresponds to the possible values for self._loc, excluding "best".

        - width, height: the final size of the legend, axes units.
        i   i   g      ?g      ?g       @(   R   R   R>   (   R4   Rk   R&   R'   t   BESTt   URt   ULt   LLt   LRt   Rt   CLt   CRt   LCt   UCt   CR   R   (    (    s:   /Users/cmoad/workspace/matplotlib/lib/matplotlib/legend.pyR     s    -c            s3  t   i  o t   i  o d  Sn    f d   } g  } x t  i d  i d   D] \ } } | i   \ } } | |  \ } }	 }
 } |	  i 8}	 | d  i 7} | i |	 | f  | i | |	 d | f  q` W|  i d  \ } }	 }
 } |	  i 8}	 | d  i 7} | i |	 | f  x t  i |  D] \ } } | \ } } t | t	  o5 | t
 i  i i t  } | i | | d  qIt | t  o* | i | d |  | i | d  qIqIW i    i   } | i d  i d  i  | i   \ } }	 }
 }  i i | |	 |
 |  d	 \ } } t  i  o\ t   i  d j oF  i i   }  i i   }  i \ } } | | | | } } n`  i d j o  i |
 |  \ } } n4  i  i |
 |  \ } } | | | |	 } }  i  | |  d  S(
   Nc            s.   |  i    } t   i   |  } | i   S(   N(   R   R   Ru   R   (   R   R#   t   bboxa(   R4   R   (    s:   /Users/cmoad/workspace/matplotlib/lib/matplotlib/legend.pyt   get_tbounds  s    i   ii   g?i   i    g      ?(   i    i    (!   R   Rc   Ra   RI   Rd   R;   R   R   RO   R	   R\   R   R^   R   R   t	   set_ydataR   R   t
   set_heightR   t   deepcopyt   scaleR9   R   Rf   t
   set_boundsR   R[   R   R   R   R   R   (   R4   R   R   t   hposR   t   taboveR   R   R   R   RC   R   R   t   tupR   R#   R   R   t   xot   yo(    (   R   R4   s:   /Users/cmoad/workspace/matplotlib/lib/matplotlib/legend.pyR|     sN    *  &N(   t   __name__t
   __module__t   __doc__RU   t   zorderR5   RJ   RH   Rg   Re   R}   R   Rb   R   R   R   R   R   R   R`   R   R   R   R   R|   (    (    (    s:   /Users/cmoad/workspace/matplotlib/lib/matplotlib/legend.pyR0   F   sR   										c					7	:								/	(/   R  t
   __future__R    t   sysRW   t   numpyR\   t
   matplotlibR   R   t   artistR   t   cbookR   R   R   R   t   font_managerR   R   R	   t   mlabR
   R   R   R   R   R   R   R   R   R   R   R   R   R   t
   transformsR   R   R   R   R   R   R   R   R/   R0   (    (    (    s:   /Users/cmoad/workspace/matplotlib/lib/matplotlib/legend.pys   <module>   s   ".:	