3
6Ad]ÓQ  ã               @   sž   d Z ddlZddlZddlZddlZddlmZmZm	Z	m
Z
 ddlmZ ddljZejeƒZG dd„ deƒZG dd„ deƒZG d	d
„ d
eƒZG dd„ dƒZdS )a`  
:mod:`~matplotlib.gridspec` contains classes that help to layout multiple
`~axes.Axes` in a grid-like pattern within a figure.

The `GridSpec` specifies the overall grid structure. Individual cells within
the grid are referenced by `SubplotSpec`\s.

See the tutorial :ref:`sphx_glr_tutorials_intermediate_gridspec.py` for a
comprehensive usage guide.
é    N)Ú_pylab_helpersÚcbookÚtight_layoutÚrcParams)ÚBboxc               @   sp   e Zd ZdZddd„Zdd„ Zdd„ Zdd	d
„Zddd„Zdd„ Z	dd„ Z
dd„ Zdd„ Zddd„Zdd„ ZdS )ÚGridSpecBasezm
    A base class of GridSpec that specifies the geometry of the grid
    that a subplot will be placed.
    Nc             C   s&   || | _ | _| j|ƒ | j|ƒ dS )z¸
        The number of rows and number of columns of the grid need to
        be set. Optionally, the ratio of heights and widths of rows and
        columns can be specified.
        N)Ú_nrowsÚ_ncolsÚset_height_ratiosÚset_width_ratios)ÚselfÚnrowsÚncolsÚheight_ratiosÚwidth_ratios© r   ú~/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.6/site-packages/matplotlib-3.1.1-py3.6-linux-x86_64.egg/matplotlib/gridspec.pyÚ__init__   s    
zGridSpecBase.__init__c             C   sN   | j d k	rd| j  nd}| jd k	r,d| j nd}dj| jj| j| j|| d�S )Nz, height_ratios=%rÚ z, width_ratios=%rz&{clsname}({nrows}, {ncols}{optionals}))Zclsnamer   r   Ú	optionals)Ú_row_height_ratiosÚ_col_width_ratiosÚformatÚ	__class__Ú__name__r   r	   )r   Z
height_argZ	width_argr   r   r   Ú__repr__)   s    zGridSpecBase.__repr__c             C   s   | j | jfS )zW
        Return a tuple containing the number of rows and columns in the grid.
        )r   r	   )r   r   r   r   Úget_geometry5   s    zGridSpecBase.get_geometryc             C   s   d S )Nr   )r   ÚfigureÚfigr   r   r   Úget_subplot_params;   s    zGridSpecBase.get_subplot_paramsé   c             C   s(   |\}}| ||| …||| …f }|S )z,Create and return a `.SubplotSpec` instance.r   )r   ÚlocÚrowspanÚcolspanÚloc1Úloc2Úsubplotspecr   r   r   Únew_subplotspec>   s    zGridSpecBase.new_subplotspecc             C   s(   |d k	rt |ƒ| jkrtdƒ‚|| _d S )NzTExpected the given number of width ratios to match the number of columns of the grid)Úlenr	   Ú
ValueErrorr   )r   r   r   r   r   r   D   s    zGridSpecBase.set_width_ratiosc             C   s   | j S )N)r   )r   r   r   r   Úget_width_ratiosJ   s    zGridSpecBase.get_width_ratiosc             C   s(   |d k	rt |ƒ| jkrtdƒ‚|| _d S )NzRExpected the given number of height ratios to match the number of rows of the grid)r(   r   r)   r   )r   r   r   r   r   r
   M   s    zGridSpecBase.set_height_ratiosc             C   s   | j S )N)r   )r   r   r   r   Úget_height_ratiosS   s    zGridSpecBase.get_height_ratiosFc                s¢  | j ƒ \}}|r*d}d}d}d}d}	d}
n.| j|ƒ}|j}|j}|j}|j}|j}	|j}
|| }|| }|||
|d    }|
| }| jdk	r¶|| t	| jƒ ‰ ‡ fdd„| jD ƒ}n
|g| }dg|g|d   }t
jt
j||gƒjƒ}|||	|d    }|	| }| jdk	�r:|| t	| jƒ ‰ ‡ fdd„| jD ƒ}n
|g| }dg|g|d   }t
jt
j||gƒjƒ}|| jdƒj\}}|| jdƒj\}}||||fS )zô
        Return lists of bottom and top position of rows, left and
        right positions of columns.

        If raw=True, then these are all in units relative to the container
        with no margins.  (used for constrained_layout).
        g        g      ð?r    Nc                s   g | ]}|ˆ  ‘qS r   r   )Ú.0Úr)Únormr   r   ú
<listcomp>w   s    z3GridSpecBase.get_grid_positions.<locals>.<listcomp>r   c                s   g | ]}|ˆ  ‘qS r   r   )r,   r-   )r.   r   r   r/   ‚   s    é   éÿÿÿÿ)r1   r0   r1   )r1   r0   )r   r   ÚleftÚrightÚbottomÚtopÚwspaceÚhspacer   ÚsumÚnpÚcumsumÚcolumn_stackÚflatr   ÚreshapeÚT)r   r   Úrawr   r   r2   r3   r4   r5   r6   r7   Zsubplot_paramsZ	tot_widthZ
tot_heightZcell_hZsep_hZcell_heightsZsep_heightsZcell_hsZcell_wZsep_wZcell_widthsZ
sep_widthsZcell_wsÚfig_topsÚfig_bottomsÚ	fig_leftsÚ
fig_rightsr   )r.   r   Úget_grid_positionsV   sH    



zGridSpecBase.get_grid_positionsc       	      C   s’   | j ƒ \}}dd„ }t|tƒrry|\}}W n tk
rF   tdƒ‚Y nX tj|||dƒ|||dƒg||fƒ\}}n|||| dƒ\}}t| ||ƒS )z,Create and return a `.SubplotSpec` instance.c             S   s¨   | }t | tƒr<| j|ƒ\}}}||kr2||d fS tdƒ‚nh| dk rL| | } d|   ko^|k n  rl| | fS |d k	r�td|› d|› d|› �ƒ‚ntd|› d|› �ƒ‚d S )Nr    z=GridSpec slice would result in no space allocated for subplotr   zindex z is out of bounds for axis z with size z) is out of bounds for GridSpec with size )Ú
isinstanceÚsliceÚindicesÚ
IndexError)ÚkeyÚsizeÚaxisZorig_keyÚstartÚstopÚ_r   r   r   Ú
_normalize�   s    

z,GridSpecBase.__getitem__.<locals>._normalizezunrecognized subplot specr   r    N)r   rE   Útupler)   r9   Úravel_multi_indexÚSubplotSpec)	r   rI   r   r   rO   Úk1Úk2Únum1Únum2r   r   r   Ú__getitem__Œ   s    
zGridSpecBase.__getitem__)NN)NN)r    r    )F)r   Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r'   r   r*   r
   r+   rD   rW   r   r   r   r   r      s   




6r   c            	   @   s^   e Zd ZdZddd„Zddddd	d
gZdd„ Zdd„ Zdd„ Zddd„Z	dd„ Z
ddd„ZdS )ÚGridSpeczš
    Specifies the geometry of the grid that a subplot can be placed in.

    The location of grid is determined by similar way as the SubplotParams.
    Nc             C   sˆ   || _ || _|| _|| _|| _|	| _|| _tj| |||
|d� | jdksT| jj	ƒ  r\d| _
n(| jjƒ  tj| jj
dtjƒ  | d�| _
dS )aá  
        The number of rows and number of columns of the grid need to be set.
        Optionally, the subplot layout parameters (e.g., left, right, etc.)
        can be tuned.

        Parameters
        ----------
        nrows : int
            Number of rows in grid.

        ncols : int
            Number or columns in grid.

        figure : `~.figure.Figure`, optional

        left, right, top, bottom : float, optional
            Extent of the subplots as a fraction of figure width or height.
            Left cannot be larger than right, and bottom cannot be larger than
            top.

        wspace : float, optional
            The amount of width reserved for space between subplots,
            expressed as a fraction of the average axis width.

        hspace : float, optional
            The amount of height reserved for space between subplots,
            expressed as a fraction of the average axis height.

        width_ratios : length *ncols* iterable, optional
            Width ratios of the columns.

        height_ratios : length *nrows* iterable, optional
            Height ratios of the rows.

        Notes
        -----
        See `~.figure.SubplotParams` for descriptions of the layout parameters.
        )r   r   NÚgridspec)ÚparentÚnameÚartist)r2   r4   r3   r5   r6   r7   r   r   r   Zget_constrained_layoutÚ
_layoutboxZinit_layoutboxÚ	layoutboxÚ	LayoutBoxÚseq_id)r   r   r   r   r2   r4   r3   r5   r6   r7   r   r   r   r   r   r   ¸   s"    *


zGridSpec.__init__r2   r4   r3   r5   r6   r7   c             C   s.   | j }y|jdƒ W n tk
r(   Y nX |S )Nr`   )Ú__dict__ÚpopÚKeyError)r   Ústater   r   r   Ú__getstate__ü   s    zGridSpec.__getstate__c             C   s   || _ d | _d S )N)rd   r`   )r   rg   r   r   r   Ú__setstate__  s    zGridSpec.__setstate__c             K   s  x:|j ƒ D ].\}}|| jkr*t| ||ƒ q
t|› d�ƒ‚q
W xÚtjjjƒ D ]Ê}xÄ|jj	j
D ]¶}t|tj
jƒsät|jtj
jƒr¨|jjƒ jƒ | krâ|jjƒ  |j|jjƒ n:t|jtj
jƒrâ|jjƒ jƒ | krâ|jjƒ  |j|jjƒ qZ|jƒ jƒ }|jƒ | krZ|jƒ  |j|jƒ qZW qJW dS )z`
        Update the current values.

        Values set to None use the rcParams value.
        z is an unknown keywordN)ÚitemsÚ_AllowedKeysÚsetattrÚAttributeErrorr   ÚGcfÚfigsÚvaluesÚcanvasr   ÚaxesrE   ÚmplZSubplotBaseZ_sharexÚget_subplotspecÚget_gridspecÚupdate_paramsÚ_set_positionÚfigboxZ_shareyÚget_topmost_subplotspec)r   ÚkwargsÚkÚvZ
figmanagerÚaxÚssr   r   r   Úupdate	  s&    


zGridSpec.updatec                sT   |dkr(dd„ ˆ j D ƒ}tjjf |Ž}ntj|jƒ}|jf ‡ fdd„ˆ j D ƒŽ |S )z–
        Return a dictionary of subplot layout parameters. The default
        parameters are from rcParams unless a figure attribute is set.
        Nc             S   s   i | ]}t d |  |“qS )zfigure.subplot.)r   )r,   r{   r   r   r   ú
<dictcomp>-  s    z/GridSpec.get_subplot_params.<locals>.<dictcomp>c                s   i | ]}t ˆ |ƒ|“qS r   )Úgetattr)r,   r{   )r   r   r   r€   2  s    )rk   rs   r   ÚSubplotParamsÚcopyÚsubplotparsr   )r   r   Úkwr„   r   )r   r   r   '  s    zGridSpec.get_subplot_paramsc                s   ‡ fdd„ˆ j D ƒS )Nc                s   g | ]}t ˆ |ƒr|‘qS r   )r�   )r,   r{   )r   r   r   r/   7  s    z<GridSpec.locally_modified_subplot_params.<locals>.<listcomp>)rk   )r   r   )r   r   Úlocally_modified_subplot_params6  s    z(GridSpec.locally_modified_subplot_paramsçHáz®Gñ?c       	   
   C   sd   t j|j| d�}d|kr"tjdƒ |dkr4t j|ƒ}t j||j||||||d�}|r`| jf |Ž dS )ap  
        Adjust subplot parameters to give specified padding.

        Parameters
        ----------

        pad : float
            Padding between the figure edge and the edges of subplots, as a
            fraction of the font-size.
        h_pad, w_pad : float, optional
            Padding (height/width) between edges of adjacent subplots.
            Defaults to *pad*.
        rect : tuple of 4 floats, optional
            (left, bottom, right, top) rectangle in normalized figure
            coordinates that the whole subplots area (including labels) will
            fit into.  Default is (0, 0, 1, 1).
        )Z	grid_specNzcThis figure includes Axes that are not compatible with tight_layout, so results might be incorrect.)ÚpadÚh_padÚw_padÚrect)r   Zget_subplotspec_listrr   r   Ú_warn_externalZget_rendererZget_tight_layout_figurer   )	r   r   Úrendererrˆ   r‰   rŠ   r‹   Zsubplotspec_listrz   r   r   r   r   9  s    


zGridSpec.tight_layout)	NNNNNNNNN)N)Nr‡   NNN)r   rX   rY   rZ   r   rk   rh   ri   r   r   r†   r   r   r   r   r   r[   ²   s      
?
 r[   c               @   s,   e Zd ZdZd	dd„Zd
dd„Zdd„ ZdS )ÚGridSpecFromSubplotSpeczx
    GridSpec whose subplot layout parameters are inherited from the
    location specified by a given SubplotSpec.
    Nc       	      C   s`   || _ || _|| _tj| ||||d� |j}|dkr<d| _n |j||jd tj	ƒ  | d�| _dS )a`  
        The number of rows and number of columns of the grid need to
        be set. An instance of SubplotSpec is also needed to be set
        from which the layout parameters will be inherited. The wspace
        and hspace of the layout can be optionally specified or the
        default values (from the figure or rcParams) will be used.
        )r   r   Nz	.gridspec)r^   r_   )
Ú_wspaceÚ_hspaceÚ_subplot_specr   r   r`   Zlayout_from_subplotspecr^   ra   rc   )	r   r   r   Úsubplot_specr6   r7   r   r   Z	subspeclbr   r   r   r   c  s    
z GridSpecFromSubplotSpec.__init__c       	      C   s‚   | j dk	r| j n|dk	r |jjntd }| jdk	r8| jn|dk	rH|jjntd }| jj|ƒ}|j\}}}}t	j
j||||||d�S )z:Return a dictionary of subplot layout parameters.
        Nzfigure.subplot.hspacezfigure.subplot.wspace)r2   r3   r4   r5   r6   r7   )r�   r„   r7   r   r�   r6   r‘   Úget_positionÚextentsrs   r   r‚   )	r   r   r7   r6   rx   r2   r4   r3   r5   r   r   r   r     s    
z*GridSpecFromSubplotSpec.get_subplot_paramsc             C   s
   | j jƒ S )zY
        Return the topmost `.SubplotSpec` instance associated with the subplot.
        )r‘   ry   )r   r   r   r   ry   �  s    z/GridSpecFromSubplotSpec.get_topmost_subplotspec)NNNN)N)r   rX   rY   rZ   r   r   ry   r   r   r   r   rŽ   ^  s
    

rŽ   c               @   sl   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	ddd„Z
dd„ Zdd„ Zdd„ Zdd„ ZdS )rR   aL  
    Specifies the location of a subplot in a `GridSpec`.

    .. note::

        Likely, you'll never instantiate a `SubplotSpec` yourself. Instead you
        will typically obtain one from a `GridSpec` using item-access.

    Parameters
    ----------
    gridspec : `~matplotlib.gridspec.GridSpec`
        The GridSpec, which the subplot is referencing.
    num1, num2 : int
        The subplot will occupy the num1-th cell of the given
        gridspec.  If num2 is provided, the subplot will span between
        num1-th cell and num2-th cell.

        The index starts from 0.
    Nc             C   sN   || _ || _|| _|jd k	rD|j}tj||jd tjƒ  | d�| _nd | _d S )Nz.ss)r]   r^   r_   )Ú	_gridspecrU   rV   r`   ra   rb   r^   rc   )r   r\   rU   rV   Zglbr   r   r   r   «  s    
zSubplotSpec.__init__c             C   s.   | j }y|jdƒ W n tk
r(   Y nX |S )Nr`   )rd   re   rf   )r   rg   r   r   r   rh   ¼  s    zSubplotSpec.__getstate__c             C   s   || _ d | _d S )N)rd   r`   )r   rg   r   r   r   ri   Ä  s    zSubplotSpec.__setstate__c             C   s   | j S )N)r•   )r   r   r   r   ru   É  s    zSubplotSpec.get_gridspecc             C   s    | j ƒ jƒ \}}||| j| jfS )a  
        Return the subplot geometry as tuple ``(n_rows, n_cols, start, stop)``.

        The indices *start* and *stop* define the range of the subplot within
        the `GridSpec`. *stop* is inclusive (i.e. for a single cell
        ``start == stop``).
        )ru   r   rU   rV   )r   ÚrowsÚcolsr   r   r   r   Ì  s    zSubplotSpec.get_geometryc             C   sX   | j ƒ }|jƒ \}}t| j|ƒ\}}| jdk	r@t| j|ƒ\}}n|}|}||||||fS )zŽ
        Return the subplot row and column numbers as a tuple
        ``(n_rows, n_cols, row_start, row_stop, col_start, col_stop)``.
        N)ru   r   ÚdivmodrU   rV   )r   r\   r   r   Z	row_startZ	col_startZrow_stopZcol_stopr   r   r   Úget_rows_columns×  s    
zSubplotSpec.get_rows_columnsFc             C   s¶   | j ƒ }|jƒ \}}tj| jdkr*| jgn
| j| jg||fƒ\}}|j|ƒ\}}	}
}|| jƒ }|	| jƒ }|
| jƒ }|| jƒ }t	j
||||ƒ}|r®||d |d ||fS |S dS )zJ
        Update the subplot position from ``figure.subplotpars``.
        Nr   )ru   r   r9   Úunravel_indexrV   rU   rD   ÚminÚmaxr   Úfrom_extents)r   r   Z
return_allr\   r   r   r–   r—   rA   r@   rB   rC   Z
fig_bottomZfig_topZfig_leftZ	fig_rightrx   r   r   r   r“   æ  s    zSubplotSpec.get_positionc             C   s"   | j ƒ }t|dƒr|jƒ S | S dS )zX
        Return the topmost `SubplotSpec` instance associated with the subplot.
        ry   N)ru   Úhasattrry   )r   r\   r   r   r   ry   ý  s    
z#SubplotSpec.get_topmost_subplotspecc             C   s8   | j | j| jft|dtƒ ƒt|dtƒ ƒt|dtƒ ƒfkS )z}
        Two SubplotSpecs are considered equal if they refer to the same
        position(s) in the same `GridSpec`.
        r•   rU   rV   )r•   rU   rV   r�   Úobject)r   Úotherr   r   r   Ú__eq__  s    zSubplotSpec.__eq__c             C   s   t | j| j| jfƒS )N)Úhashr•   rU   rV   )r   r   r   r   Ú__hash__  s    zSubplotSpec.__hash__c             K   s   t ||| f|ŽS )aÜ  
        Create a GridSpec within this subplot.

        The created `.GridSpecFromSubplotSpec` will have this `SubplotSpec` as
        a parent.

        Parameters
        ----------
        nrows : int
            Number of rows in grid.

        ncols : int
            Number or columns in grid.

        Returns
        -------
        gridspec : `.GridSpecFromSubplotSpec`

        Other Parameters
        ----------------
        **kwargs
            All other parameters are passed to `.GridSpecFromSubplotSpec`.

        See Also
        --------
        matplotlib.pyplot.subplots

        Examples
        --------
        Adding three subplots in the space occupied by a single subplot::

            fig = plt.figure()
            gs0 = fig.add_gridspec(3, 1)
            ax1 = fig.add_subplot(gs0[0])
            ax2 = fig.add_subplot(gs0[1])
            gssub = gs0[2].subgridspec(1, 3)
            for i in range(3):
                fig.add_subplot(gssub[0, i])
        )rŽ   )r   r   r   rz   r   r   r   Úsubgridspec  s    (zSubplotSpec.subgridspec)N)F)r   rX   rY   rZ   r   rh   ri   ru   r   r™   r“   ry   r¡   r£   r¤   r   r   r   r   rR   —  s   


rR   )rZ   rƒ   ÚloggingÚnumpyr9   Ú
matplotlibrs   r   r   r   r   Úmatplotlib.transformsr   Úmatplotlib._layoutboxr`   ra   Ú	getLoggerr   Ú_logrŸ   r   r[   rŽ   rR   r   r   r   r   Ú<module>
   s   

  -9