ó
 m[c           @   s·   d  Z  d d l Z d d l Z d d l m Z m Z d d l m Z e j Z d „  Z	 d „  Z
 d „  Z d „  Z d d	 d d d d
 „ Z d „  Z d d „ Z d	 d d d d „ Z d S(   s	  
This module provides routines to adjust subplot params so that subplots are
nicely fit in the figure. In doing so, only axis labels, tick labels, axes
titles and offsetboxes that are anchored to axes are currently considered.

Internally, it assumes that the margins (left_margin, etc.) which are
differences between ax.get_tightbbox and ax.bbox are independent of axes
position. This may fail if Axes.adjustable is datalim. Also, This will fail
for some cases (for example, left or right margin is affected by xlabel).
iÿÿÿÿN(   t   TransformedBboxt   Bbox(   t   FontPropertiesc         C   s   | j  |  j  S(   N(   t   xmin(   t
   tight_bboxt	   axes_bbox(    (    s6   lib/python2.7/site-packages/matplotlib/tight_layout.pyt	   _get_left   s    c         C   s   |  j  | j  S(   N(   t   xmax(   R   R   (    (    s6   lib/python2.7/site-packages/matplotlib/tight_layout.pyt
   _get_right   s    c         C   s   | j  |  j  S(   N(   t   ymin(   R   R   (    (    s6   lib/python2.7/site-packages/matplotlib/tight_layout.pyt   _get_bottom   s    c         C   s   |  j  | j  S(   N(   t   ymax(   R   R   (    (    s6   lib/python2.7/site-packages/matplotlib/tight_layout.pyt   _get_top!   s    gHáz®Gñ?c
   -   
      sê  | \ }
 ‰  t  d t d ƒ j ƒ  d } | | } | d k	 rL | | } n | } | d k	 rk | | } n | } t | ƒ t | ƒ k s› t | ƒ d k r¤ t ‚ n  |	 d k rÅ d } } } } nD |	 \ } } } } | rê d | } n d } | rd | } n d } g  t |
 d ˆ  ƒ D] } g  ^ q} g  t |
 ˆ  d ƒ D] } g  ^ qE‰ t j } | d k r¾g  } xI | D]> } | g  | D] } | j	 d t
 ƒ ^ q‰ƒ } | j | ƒ qyWn  xFt | | | ƒ D]2\ } } \ } } t g  | D] } | j ƒ  ^ qðƒ rqÑn  | g  | D]! } | j ƒ  r| j | ƒ ^ qƒ } t | |  j j ƒ  ƒ } t | ˆ  ƒ \ } }  | d k rˆ | ˆ  d |  j t | | ƒ ƒ ˆ | ˆ  d |  d j t | | ƒ ƒ | | ˆ  |  j t | | ƒ ƒ | | d ˆ  |  j t | | ƒ ƒ qÑt | ˆ  ƒ \ }! }" xk t | |! d ƒ D]V }# ˆ |# ˆ  d |  j t | | ƒ ƒ ˆ |# ˆ  d |" d j t | | ƒ ƒ qCWxc t |  |" d ƒ D]N }$ | | ˆ  |$ j t | | ƒ ƒ | |! d ˆ  |$ j t | | ƒ ƒ q±WqÑW|  j ƒ  \ }% }& | smt g  ˆ d d ˆ  d … D] }' t |' ƒ ^ q:d g ƒ } | | |% 7} n  | sÁt g  ˆ ˆ  d ˆ  d … D] }' t |' ƒ ^ qŽd g ƒ } | | |% 7} n  | st g  | ˆ   D] }' t |' ƒ ^ qÕd g ƒ } | | |& 7} n  | sPt g  | ˆ  D] }' t |' ƒ ^ qd g ƒ } | | |& 7} n  | | d k r|d } d } t j d	 ƒ n  | | d k r¨d } d } t j d
 ƒ n  t d | d d | d | d d | ƒ }( ˆ  d k r_t ‡  ‡ f d †  t |
 ƒ Dƒ ƒ | |% }) d | | |) ˆ  d ˆ  }* |* d k  rNt j d ƒ d |( d <q_|) |* |( d <n  |
 d k ræt d „  | ˆ  ˆ  !Dƒ ƒ | |& }+ d | | |+ |
 d |
 }, |, d k  rÕt j d ƒ d |( d <qæ|+ |, |( d <n  |( S(   s'  
    Return a dict of subplot parameters to adjust spacing between subplots.

    Note that this function ignores geometry information of subplot
    itself, but uses what is given by the *nrows_ncols* and *num1num2_list*
    parameters.  Also, the results could be incorrect if some subplots have
    ``adjustable=datalim``.

    Parameters
    ----------
    nrows_ncols : Tuple[int, int]
        Number of rows and number of columns of the grid.
    num1num2_list : List[int]
        List of numbers specifying the area occupied by the subplot
    subplot_list : list of subplots
        List of subplots that will be used to calculate optimal subplot_params.
    pad : float
        Padding between the figure edge and the edges of subplots, as a
        fraction of the font size.
    h_pad, w_pad : float
        Padding (height/width) between edges of adjacent subplots, as a
        fraction of the font size.  Defaults to *pad*.
    rect : Tuple[float, float, float, float]
        [left, bottom, right, top] in normalized (0, 1) figure coordinates.
    t   sizes	   font.sizeiH   i    i   t   originalNg<NÑ‘\þß?s\   The left and right margins cannot be made large enough to accommodate all axes decorations. s\   The bottom and top margins cannot be made large enough to accommodate all axes decorations. t   leftt   rightt   bottomt   topc         3   sK   |  ]A } ˆ | ˆ  d  d  | d  ˆ  d  d  !D] } t  | ƒ Vq0 q d S(   i   N(   t   sum(   t   .0t   it   s(   t   colst   hspaces(    s6   lib/python2.7/site-packages/matplotlib/tight_layout.pys	   <genexpr>¿   s   sT   tight_layout cannot make axes width small enough to accommodate all axes decorationsg      à?t   wspacec         s   s   |  ] } t  | ƒ Vq d  S(   N(   R   (   R   R   (    (    s6   lib/python2.7/site-packages/matplotlib/tight_layout.pys	   <genexpr>Í   s    sU   tight_layout cannot make axes height small enough to accommodate all axes decorationst   hspace(   R   t   rcParamst   get_size_in_pointst   Nonet   lent
   ValueErrort   rangeR   t   uniont   get_positiont   Truet   appendt   zipt   allt   get_visiblet   get_tightbboxR    t   transFiguret   invertedt   divmodR   R   R   R
   t   get_size_inchest   maxR   t   warningst   warnt   dict(-   t   figt   renderert   nrows_ncolst   num1num2_listt   subplot_listt   ax_bbox_listt   padt   h_padt   w_padt   rectt   rowst   font_size_inchest
   pad_inchest   vpad_inchest   hpad_inchest   margin_leftt   margin_bottomt   margin_rightt
   margin_topt   _rightt   _topR   t   vspacesR!   t   subplotst   axt   ax_bboxt   num1t   num2t   tight_bbox_rawR   t   row1t   col1t   row2t   col2t   row_it   col_it   fig_width_incht   fig_height_inchR   t   kwargsR   t   h_axest   vspacet   v_axes(    (   R   R   s6   lib/python2.7/site-packages/matplotlib/tight_layout.pyt   auto_adjust_subplotpars%   sÌ    
*	''	%	&$==01
c         C   s{   |  j  r |  j  } nb |  j } | rB t | d ƒ rB | j ƒ  } n5 t j d ƒ d d l m } | |  ƒ } | j ƒ  } | S(   Nt   get_renderers+   tight_layout : falling back to Agg rendereriÿÿÿÿ(   t   FigureCanvasAgg(   t   _cachedRenderert   canvast   hasattrRZ   R.   R/   t   matplotlib.backends.backend_aggR[   (   R1   R2   R]   R[   (    (    s6   lib/python2.7/site-packages/matplotlib/tight_layout.pyRZ   Ú   s    		c         C   s¾   g  } x± |  D]© } | j  ƒ  } | d k r4 | } n  t | d ƒ r£ | j ƒ  } | j ƒ  } | j ƒ  } | d k	 r‹ | | k r  d } q  q© | j ƒ  r© d } q© n d } | j | ƒ q W| S(   sù   Return a list of subplotspec from the given list of axes.

    For an instance of axes that does not support subplotspec, None is inserted
    in the list.

    If grid_spec is given, None is inserted for those not from the given
    grid_spec.
    t   get_subplotspecN(   t   get_axes_locatorR   R^   R`   t   get_topmost_subplotspect   get_gridspect   locally_modified_subplot_paramsR$   (   t	   axes_listt	   grid_spect   subplotspec_listRH   t   axes_or_locatort   subplotspect   gs(    (    s6   lib/python2.7/site-packages/matplotlib/tight_layout.pyt   get_subplotspec_listì   s"    		c   (      C   sZ  g  } g  }	 g  }
 g  } i  } g  } x¹ t  | | ƒ D]¨ \ } } | d k rR q4 n  | j | g  ƒ } | sÏ | j ƒ  \ } } } } |	 j | ƒ |
 j | ƒ | j | ƒ | j | ƒ | j | j |  ƒ ƒ n  | j | ƒ q4 Wt |	 ƒ d k st |
 ƒ d k ri  St |	 ƒ } t |
 ƒ } g  } x | D]ø } | j ƒ  \ } } } } t | | ƒ \ } } t | | ƒ \ } } | d k s| d k rœt	 d ƒ ‚ n  t | | ƒ \ } }  | d k rÍ| |  }! }" n t | | ƒ \ }! }" | j | | | |  | |! d | d | |" d | d f ƒ q-Wt
 |  | d | | f d | d | d | d | d	 | d
 | ƒ}# | d k	 rV| \ }$ }% }& }' |$ d k	 r£|$ |# d 7}$ n  |% d k	 rÀ|% |# d 7}% n  |& d k	 rá|& d |# d 8}& n  |' d k	 r|' d |# d 8}' n  t
 |  | d | | f d | d | d | d | d	 | d
 | d |$ |% |& |' f ƒ}# n  |# S(   s  
    Return subplot parameters for tight-layouted-figure with specified padding.

    Parameters
    ----------
    fig : Figure
    axes_list : list of Axes
    subplotspec_list : list of `.SubplotSpec`
        The subplotspecs of each axes.
    renderer : renderer
    pad : float
        Padding between the figure edge and the edges of subplots, as a
        fraction of the font size.
    h_pad, w_pad : float
        Padding (height/width) between edges of adjacent subplots.  Defaults to
        *pad_inches*.
    rect : Tuple[float, float, float, float], optional
        (left, bottom, right, top) rectangle in normalized figure coordinates
        that the whole subplots area (including labels) will fit into.
        Defaults to using the entire figure.
    i    t    i   R3   R4   R5   R6   R7   R8   R9   R   R   R   R   R:   N(   R%   R   t
   setdefaultt   get_geometryR$   R"   R   R-   R+   t   RuntimeErrorRY   ((   R1   Re   Rg   R2   R7   R8   R9   R:   R5   t
   nrows_listt
   ncols_listR6   t   subplot_dictt   subplotspec_list2RH   Ri   RG   t   myrowst   mycolst   _t	   max_nrowst	   max_ncolsR4   R;   R   RJ   RK   t   div_rowt   mod_rowt   div_colt   mod_colt   rowNum1t   colNum1t   rowNum2t   colNum2RU   R   R   R   R   (    (    s6   lib/python2.7/site-packages/matplotlib/tight_layout.pyt   get_tight_layout_figure  sz    	$.	(   t   __doc__R.   t
   matplotlibt   matplotlib.transformsR    R   t   matplotlib.font_managerR   R   R   R   R
   R   R   RY   RZ   Rk   R   (    (    (    s6   lib/python2.7/site-packages/matplotlib/tight_layout.pyt   <module>
   s   					³	!