
 m[c           @` s   d  Z  d d l m Z m Z m Z m Z d d l Z d d l Z d d l	 Z	 d d l
 m Z d d l j Z d d l j Z e j e  Z d   Z d   Z d   Z e e d  Z d d d	  Z d
 d  Z d
 d  Z d S(   u  
This module provides the routine to adjust subplot layouts so that there are
no overlapping axes or axes decorations.  All axes decorations are dealt with
(labels, ticks, titles, ticklabels) and some dependent artists are also dealt
with (colorbar, suptitle, legend).

Layout is done via :meth:`~matplotlib.gridspec`, with one constraint per
gridspec, so it is possible to have overlapping axes if the gridspecs
overlap (i.e. using :meth:`~matplotlib.gridspec.GridSpecFromSubplotSpec`).
Axes placed using ``figure.subplots()`` or ``figure.add_subplots()`` will
participate in the layout.  Axes manually placed via ``figure.add_axes()``
will not.

See Tutorial: :doc:`/tutorials/intermediate/constrainedlayout_guide`

i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsN(   t   Legendc         C` si   |  j  d |  } xP |  j   D]B } t | t  r | j j |  } t j j | | g  } q q W| S(   ui   
    Get the tight_bbox of the axis ax, and any dependent decorations, like
    a `Legend` instance.
    t   renderer(	   t   get_tightbboxt   get_childrent
   isinstanceR   t   _legend_boxt   get_window_extentt
   transformst   Bboxt   union(   t   axR   t   bboxt   childt   bboxn(    (    s=   lib/python2.7/site-packages/matplotlib/_constrained_layout.pyt   get_axall_tightbbox>   s    c         C` s<   |  | k r |  | k r t  S| | k r8 | | k r8 t  St S(   N(   t   Truet   False(   t
   colnum0mint
   colnum0maxt
   colnumCmint
   colnumCmax(    (    s=   lib/python2.7/site-packages/matplotlib/_constrained_layout.pyt   in_same_columnO   s
    c         C` s<   |  | k r |  | k r t  S| | k r8 | | k r8 t  St S(   N(   R   R   (   t
   rownum0mint
   rownum0maxt
   rownumCmint
   rownumCmax(    (    s=   lib/python2.7/site-packages/matplotlib/_constrained_layout.pyt   in_same_rowW   s
    c   ;      C` s  |  j  j   j } t g   } xT |  j D]I } t | d  r( | j   j   }	 |	 j d k	 rq | j
 |	  qq q( q( Wt |  d k r t j d  n  |  j j d k  rx[| D]P}	 |	 j   \ }
 } t j |
 | d t } g  } xW |  j D]L } t | d  r | j d k	 r | j   j   |	 k r | | g 7} q q WxO | D]G } | j   } | j d k rx| j | _ n  t | | j | j d +qHWxj t |  D]\ \ } } | s|  j |	 |  } | j t  | j g   | j g   | j d  qqWq Wn  x=|  j D]2} t j | j  | j d k	 r| j d t  } t  | |  } | |  } | j! j" } | d k r| } n  | j! j# } | d k r| } n  | j! j$ | j% | j% |  | j! j& | j' | j' |  | j! j( | j) | j) |  | j! j* | j+ | j+ |  t j d | j% | j% |  t j d | j' | j' |  t j d	 | j) | j) |  |  j j d k  rC| j! j, d
 d d | j! j- d
 d d | j j, d
 d d | j j- d
 d d | j! j. d d d | j! j/ d d d | j! j0 d d d | j! j1 d d d qCqqW|  j2 d k	 r|  j2 j d k	 r|  j2 } | | j3 d |   } | j+ | j) } | j j4 | |  n  |  j j d k  re|  j } x6 | j5 D]+ } | j6   rt7 | d | d | qqWx^| D]S}	 |	 j   \ }
 } |	 j8   } |	 j9   } | d k rYt j: |  } n  | d k rwt j: |
  } n  g  } xZ |  j D]O } t | d  r| j d k	 r| j   j   |	 k r| | g 7} qqqWt j t |  d t j; } t j t |  d t j; } t j t |  d t j; } t j t |  d t j; } t j t |   }  t j t |   } x t |  D] \ }! } | j   } | j d k r| j | _ n  t< | j |  \ | |! <| |! <t< | j |  \ | |! <| |! <t j= | | |! | |! d ! |  |! <t j= | | |! | |! d ! | |! <qWxt | d   D]\ } } | j   } | | | | }" }# | | | | }$ }% |  | | | }& }' t }( t }) t }* t }+ t }, t }- x|t> | d t |   D]a}. | |. }/ | |. | |. }0 }1 | |. | |. }2 }3 |  |. | |. }4 }5 |( r||# |1 k r|t? j@ | j! |/ j! g d  t }( n  |) r|% |3 k rt? j@ | j! |/ j! g d  t }) n  |+ r|" |0 k rt j d  t? j@ | j! |/ j! g d  t }+ n  |* r>	|$ |2 k r>	t j d  t? j@ | j! |/ j! g d  t }* n  |2 |0 d }6 |$ |" d }7 |3 |1 d }8 |% |# d }9 |, r	|7 |6 k r	| j! jA |/ j! jB |' |5  t }, n tC |# |% |1 |3  rf
|' |5 k r
| j! j, |/ j! jB |' |5  |/ j! j, | j! jB |5 |' d  qf
|' |5 k  rf
|/ j! j, | j! jB |5 |'  | j! j, | j! jB |' |5 d  qf
n  |- r
|9 |8 k r
| j! jD |/ j! jE |& |4  t }- qtF |" |$ |0 |2  r|& |4 k r| j! j- |/ j! jE |& |4  |/ j! j- | j! jE |4 |& d  qV|& |4 k  rV|/ j! j- | j! jE |4 |&  | j! j- |/ j! jE |& |4 d  qVqqWq`WqWn  |  j j d 7_ |  j jG   xU |  j D]J } | j d k	 r| j! jH   }: t j d |:  | jI |: d d qqWd S(   u  
    Do the constrained_layout.  Called at draw time in
     ``figure.constrained_layout()``

    Parameters
    ----------


    fig: Figure
      is the ``figure`` instance to do the layout in.

    renderer: Renderer
      the renderer to use.

     h_pad, w_pad : float
       are in figure-normalized units, and are a padding around the axes
       elements.

     hspace, wspace : float
        are in fractions of the subplot sizes.

    u   get_subplotspeci    ug   There are no gridspecs with layoutboxes. Possibly did not call parent GridSpec with the figure= keywordi   t   dtypet   originalu   left %fu   right %fu	   bottom %fi   t   strengthu   weakR   t   hspacet   wspaceiu   leftu   rightu   rownum0min == rownumCminu   topu   rownum0max == rownumCmaxu   bottomg?u	   newpos %rt   whichu   originalN(   i   i    i    i    (J   t   transFiguret   invertedt   transform_bboxt   sett   axest   hasattrt   get_subplotspect   get_gridspect
   _layoutboxt   Nonet   addt   lent   warningst   warnt   constrained_layout_calledt   get_geometryt   npt   zerost   boolt   num2t   num1R   t	   enumeratet   add_subplott   set_frame_onR   t
   set_xtickst
   set_ytickst   set_facecolort   _logt   debugt   get_positionR   t   _poslayoutboxt   h_padt   w_padt   edit_left_margin_mint   x0t   edit_right_margin_mint   x1t   edit_bottom_margin_mint   y0t   edit_top_margin_mint   y1t   constrain_height_mint   constrain_width_mint   constrain_top_margint   constrain_bottom_margint   constrain_right_margint   constrain_left_margint	   _suptitleR
   t   edit_heightt   childrent   _is_gridspec_layoutboxt   _arange_subplotspecst   get_width_ratiost   get_height_ratiost   onest   int8t   divmodt   sumt   ranget	   layoutboxt   alignt   constrain_heightt   heightR   t   constrain_widtht   widthR   t   update_variablest   get_rectt   _set_position(;   t   figR   RD   RE   R"   R#   t   invTransFigt   gssR   t   gst   nrowst   ncolst   hassubplotspect   axst   ss0t   nnt   hsst   post	   tightbboxR   t   h_padtt   w_padtt   supRc   t   figlbR   t   width_ratiost   height_ratiost	   rownummint	   rownummaxt	   colnummint	   colnummaxRe   t   nR   R   R   R   t   width0t   height0t	   alignleftt
   alignrightt   alignbott   aligntopt   alignheightt
   alignwidtht   mmt   axcR   R   R   R   t   widthCt   heightCt   drowsCt   drows0t   dcolsCt   dcols0t   newpos(    (    s=   lib/python2.7/site-packages/matplotlib/_constrained_layout.pyt   do_constrained_layout`   st   A						 !	
	   $ 
																				!c         C` s1  g  } xf |  j  D][ } | j   r x6 | j  D]+ } | j   r, t | d | d | q, q, W| | g 7} q q Wx| D]} | j } | j   j   \ } }	 | j d k r | j	 | _ n  t
 | j	 |	  \ }
 } t
 | j |	  \ } } | d } x1| D])} | j } t
 | j	 |	  \ } } | j d k rA| j	 | _ n  t
 | j |	  \ } } | |	 } | | k  rt j | j | j g d | n  | | k  rt j | j | j g d | n  | | } | | k  rt j | j | j g d | n  | |
 k  r t j | j | j g d | q q Wqv Wd S(   u   
    arange the subplotspec children of this gridspec, and then recursively
    do the same of any gridspec children of those gridspecs...
    R"   R#   i   t   paddingN(   RV   t   _is_subplotspec_layoutboxRW   RX   t   artistR,   R4   R8   R.   R9   R]   R`   t   hstackR-   t   vstack(   Rl   R"   R#   t
   sschildrenR   t   child2t   child0Rq   Rm   Rn   t
   rowNum0mint
   colNum0mint
   rowNum0maxt
   colNum0maxt   childct   ssct
   rowNumCmint
   colNumCmint
   rowNumCmaxt
   colNumCmaxt   thepad(    (    s=   lib/python2.7/site-packages/matplotlib/_constrained_layout.pyRX     sJ    	
	




g?c         C` s*  |  j  } |  j } |  j   j  } t j d | d | j d d |  }	 | d k r5t j d |	 d |	 j d d t d	 t d
 t d |  }
 | d k r t j | |	 g d | | j	 d d n  t j |	 | g d | | j	 t j
 | |
 g d | g  t j | |
 g d  |
 j | | j d | d d n | d k r t j d |	 d |	 j d d t d	 t d
 t d |  }
 | d k rt j | |	 g d | | j n  t j |	 | g d | | j t j | |
 g d | g d d t j | |
 g d  |
 j | j	 | | d d n  |	 |
 f S(   u   
    Do the layout for a colorbar, to not oeverly pollute colorbar.py

    `pad` is in fraction of the original axis size.
    t   parentt   nameu   .cbarR   u   leftu   rightu   .post
   tightwidthRt   t   subplotR   R!   u   strongi   u   v_centeru   bottomu   topt   tightheightu   h_centeru   medium(   u   leftu   right(   u   bottomu   top(   R-   RC   R+   R`   t	   LayoutBoxR   R   R   R   Re   t   match_heightsRa   Rd   Rc   R   t   match_widthsRb   (   R   t   caxt   shrinkt   aspectt   locationt   padt   axlbt   axpost   axsslbt   lbt   lbpos(    (    s=   lib/python2.7/site-packages/matplotlib/_constrained_layout.pyt   layoutcolorbarsingle  sP    						
 
		# 
c         C` s  |  d j    j   } | j } t j d | j d | j j d d |  } | d k rt j d | d | j d d	 t d
 t d t d |  }	 | d k r t j	 | | g d | | j
 d d n  t j	 | | g d | | j
 d }
 d } d } d } x |  D] } | j    } | j   j   \ } } xh | j | j g D]T } t | j |  \ } } | |
 k rw| }
 | } n  | | k  r>| } | } q>q>WqW| j } | j } |	 j | j | j | d d |	 j | j | j d | d | j d d |	 j |	 j | | d d n| d k rt j d | d | j d d t d
 t d t d |  }	 | d k rt j | | g d | | j
 n  t j | | g d | | j
 d } d } d } d } x |  D] } | j    } | j   j   \ } } xh | j | j g D]T } t | j |  \ } } | | k rE| } | } n  | | k  r| } | } qqWqW| j } | j } |	 j | j | j |  |	 j | j | j d | d | j  |	 j |	 j
 | | d d n  | |	 f S(   u   
    Do the layout for a colorbar, to not oeverly pollute colorbar.py

    `pad` is in fraction of the original axis size.
    i    R   R   u   .cbarR   u   leftu   rightu   .posR   Rt   R   R   R!   u   strongi`yi@B i   i   u   bottomu   topR   u   medium(   u   leftu   rightN(   u   bottomu   top(   R+   R,   R-   R`   R   R   R   R   R   R   Re   R.   R4   R9   R8   R]   RC   Rb   t   topt   bottomt   constrain_bottomRd   Rc   R   t   rightt   leftt   constrain_left(   t   parentsR   R   R   R   R   Rl   t   gslbR   R   t   maxrowt   minrowt   maxaxt   minaxR   t   subspecRm   Rn   t   numt   rownum1t   colnum1t   maxposlbt   minposlbt   maxcolt   mincol(    (    s=   lib/python2.7/site-packages/matplotlib/_constrained_layout.pyt   layoutcolorbargridspec,  s    				
 			#
		# 			$
(   t   __doc__t
   __future__R    R   R   R   t   numpyR5   t   loggingR1   t   matplotlib.legendR   t   matplotlib.transformsR   t   matplotlib._layoutboxR-   R`   t	   getLoggert   __name__R@   R   R   R   R.   R   RX   R   R   (    (    (    s=   lib/python2.7/site-packages/matplotlib/_constrained_layout.pyt   <module>   s     "			
 ^3;