B
    ]t\XN                 @   s   d Z ddl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mZ eeZG dd deZG dd deZG d	d
 d
eZG dd deZdS )a  
:mod:`~matplotlib.gridspec` is a module which specifies the location
of the subplot in the figure.

    `GridSpec`
        specifies the geometry of the grid that a subplot will be
        placed. 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.

    `SubplotSpec`
        specifies the location of the subplot in the given `GridSpec`.

    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&   || | _ | _| | | | 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   2lib/python3.7/site-packages/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   Z	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 )z'get the geometry of the grid, e.g., 2,3)r   r	   )r   r   r   r   get_geometry:   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 SuplotSpec instance.
        r   )r   ZlocZrowspanZcolspanZloc1Zloc2Zsubplotspecr   r   r   new_subplotspecA   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   I   s    zGridSpecBase.set_width_ratiosc             C   s   | j S )N)r   )r   r   r   r   get_width_ratiosO   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
   R   s    zGridSpecBase.set_height_ratiosc             C   s   | j S )N)r   )r   r   r   r   get_height_ratiosX   s    zGridSpecBase.get_height_ratiosFc                s  |   \}}|r*d}d}d}d}d}	d}
n.| |}|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
t
||gj}|||	|d    }|	| }| jdk	r:|| t	| j   fdd| jD }n
|g| }dg|g|d   }t
t
||gj}|| d	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>|   s    z3GridSpecBase.get_grid_positions.<locals>.<listcomp>r   c                s   g | ]}|  qS r   r   )r%   r&   )r'   r   r   r(      s    )   )r   r   leftrightbottomtopwspacehspacer   sumnpZcumsumZcolumn_stackZflatr   ZreshapeT)r   r   rawr   r   r+   r,   r-   r.   r/   r0   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_positions[   sH    



zGridSpecBase.get_grid_positionsc       	      C   s   |   \}}dd }t|trny|\}}W n tk
rF   tdY nX t||||||g||f\}}n|||| \}}t| ||S )z1Create and return a SuplotSpec instance.
        c             S   sl   t | tr0| |\}}}||kr`||d fS n0| dk r@| |7 } d|   krT|k r`n n| | fS tdd S )Nr   r   zinvalid index)
isinstancesliceindices
IndexError)keysizestartstop_r   r   r   
_normalize   s    
z,GridSpecBase.__getitem__.<locals>._normalizezunrecognized subplot spec)r   r:   tupler"   r2   Zravel_multi_indexSubplotSpec)	r   r>   r   r   rC   Zk1Z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$   r9   rH   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
    A class that specifies the geometry of the grid that a subplot
    will be placed. The location of grid is determined by similar way
    as the SubplotParams.
    Nc             C   s   || _ || _|| _|| _|| _|	| _|| _tj| |||
|d | jdksR| j	 sZd| _
n(| j  tj| jj
dt  | 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
            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
            The amount of width reserved for space between subplots,
            expressed as a fraction of the average axis width.

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

        Notes
        -----
        See `~.figure.SubplotParams` for descriptions of the layout parameters.
        )r   r   Ngridspec)parentnameartist)r+   r-   r,   r.   r/   r0   r   r   r   Zget_constrained_layout
_layoutboxZinit_layoutbox	layoutbox	LayoutBoxseq_id)r   r   r   r   r+   r-   r,   r.   r/   r0   r   r   r   r   r   r      s"    $


zGridSpec.__init__r+   r-   r,   r.   r/   r0   c             C   s.   | j }y|d W n tk
r(   Y nX |S )NrQ   )__dict__popKeyError)r   stater   r   r   __getstate__   s    zGridSpec.__getstate__c             C   s   || _ d | _d S )N)rU   rQ   )r   rX   r   r   r   __setstate__   s    zGridSpec.__setstate__c             K   s  x:|  D ].\}}|| jkr*t| || q
td|f q
W xtjj D ]}x|jj	j
D ]}t|tj
jst|jtj
jr|j  | kr|j  ||jj n:t|jtj
jr|j  | kr|j  ||jj qZ|  }| | krZ|  ||j qZW qJW dS )z
        Update the current values.  If any kwarg is None, default to
        the current value, if set, otherwise to rc.
        z%s is unknown keywordN)items_AllowedKeyssetattrAttributeErrorr   ZGcfZfigsvaluesZcanvasr   axesr:   mplZSubplotBaseZ_sharexZget_subplotspecget_gridspecZupdate_paramsZ_set_positionfigboxZ_shareyget_topmost_subplotspec)r   kwargskvZ
figmanagerZaxZssr   r   r   update  s&    


zGridSpec.updatec                sz   |dk	rt jddddd |dkr&|}|dkrNdd  jD }tjjf |}nt|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.
        Nz2.2r   zkeyword argumentr   )obj_typealternativec             S   s   i | ]}t d |  |qS )zfigure.subplot.)r   )r%   rf   r   r   r   
<dictcomp>,  s    z/GridSpec.get_subplot_params.<locals>.<dictcomp>c                s   i | ]}t  ||qS r   )getattr)r%   rf   )r   r   r   rk   1  s    )	r   warn_deprecatedr\   ra   r   SubplotParamscopysubplotparsrh   )r   r   r   kwrp   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   )rl   )r%   rf   )r   r   r   r(   6  s    z<GridSpec.locally_modified_subplot_params.<locals>.<listcomp>)r\   )r   r   )r   r   locally_modified_subplot_params5  s    z(GridSpec.locally_modified_subplot_paramsHzG?c       	   
   C   sd   t j|j| d}d|kr"td |dkr4t |}t j||j||||||d}|r`| jf | dS )ay  
        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_inches``.
        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_listr`   warningswarnZget_rendererZget_tight_layout_figurerh   )	r   r   Zrendererrt   ru   rv   rw   Zsubplotspec_listre   r   r   r   r   8  s    


zGridSpec.tight_layout)	NNNNNNNNN)NN)Nrs   NNN)r   rI   rJ   rK   r   r\   rY   rZ   rh   r   rr   r   r   r   r   r   rL      s      
9
 rL   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	  | 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)rO   rP   )
_wspace_hspace_subplot_specr   r   rQ   Zlayout_from_subplotspecrO   rR   rT   )	r   r   r   Zsubplot_specr/   r0   r   r   Z	subspeclbr   r   r   r   a  s    
z GridSpecFromSubplotSpec.__init__c       
      C   s   |dk	rt jddddd |dkr&|}| jdk	r6| jn|dk	rF|jjntd }| jdk	r^| jn|dk	rn|jjntd }| j	|}|j
\}}}}	tjj||||	||d	S )
z:Return a dictionary of subplot layout parameters.
        Nz2.2r   zkeyword argumentr   )ri   rj   zfigure.subplot.hspacezfigure.subplot.wspace)r+   r,   r-   r.   r/   r0   )r   rm   r|   rp   r0   r   r{   r/   r}   get_positionZextentsra   r   rn   )
r   r   r   r0   r/   rc   r+   r-   r,   r.   r   r   r   r   }  s     

z*GridSpecFromSubplotSpec.get_subplot_paramsc             C   s
   | j  S )zAGet the topmost SubplotSpec instance associated with the subplot.)r}   rd   )r   r   r   r   rd     s    z/GridSpecFromSubplotSpec.get_topmost_subplotspec)NNNN)NN)r   rI   rJ   rK   r   r   rd   r   r   r   r   rz   \  s
    

rz   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 )rE   zCSpecifies the location of the subplot in the given `GridSpec`.
    Nc             C   sN   || _ || _|| _|jdk	rD|j}tj||jd t  | d| _nd| _dS )z
        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.
        Nz.ss)rN   rO   rP   )	_gridspecrF   rG   rQ   rR   rS   rO   rT   )r   rM   rF   rG   Zglbr   r   r   r     s    
zSubplotSpec.__init__c             C   s.   | j }y|d W n tk
r(   Y nX |S )NrQ   )rU   rV   rW   )r   rX   r   r   r   rY     s    zSubplotSpec.__getstate__c             C   s   || _ d | _d S )N)rU   rQ   )r   rX   r   r   r   rZ     s    zSubplotSpec.__setstate__c             C   s   | j S )N)r   )r   r   r   r   rb     s    zSubplotSpec.get_gridspecc             C   s    |    \}}||| j| jfS )z
        Get the subplot geometry (``n_rows, n_cols, start, stop``).

        start and stop are the index of the start and stop of the
        subplot.
        )rb   r   rF   rG   )r   rowscolsr   r   r   r     s    zSubplotSpec.get_geometryc             C   sX   |   }| \}}t| j|\}}| jdk	r@t| j|\}}n|}|}||||||fS )z
        Get the subplot row and column numbers:
        (``n_rows, n_cols, row_start, row_stop, col_start, col_stop``)
        N)rb   r   divmodrF   rG   )r   rM   r   r   Z	row_startZ	col_startZrow_stopZcol_stopr   r   r   get_rows_columns  s    
zSubplotSpec.get_rows_columnsFc             C   s   |   }| \}}t| jdkr*| jgn
| j| jg||f\}}||\}}	}
}||  }|	|  }|
|  }||  }t	
||||}|r||d |d ||fS |S dS )zAUpdate the subplot position from ``figure.subplotpars``.
        Nr   )rb   r   r2   Zunravel_indexrG   rF   r9   minmaxr   Zfrom_extents)r   r   Z
return_allrM   r   r   r   r   r6   r5   r7   r8   Z
fig_bottomZfig_topZfig_leftZ	fig_rightrc   r   r   r   r~     s    zSubplotSpec.get_positionc             C   s"   |   }t|dr| S | S dS )z@get the topmost SubplotSpec instance associated with the subplotrd   N)rb   hasattrrd   )r   rM   r   r   r   rd     s    
z#SubplotSpec.get_topmost_subplotspecc             C   s8   | j | j| jft|dt t|dt t|dt fkS )Nr   rF   rG   )r   rF   rG   rl   object)r   otherr   r   r   __eq__  s    zSubplotSpec.__eq__c             C   s   t | j| j| jfS )N)hashr   rF   rG   )r   r   r   r   __hash__  s    zSubplotSpec.__hash__c             K   s   t ||| f|S )a  
        Return a `.GridSpecFromSubplotSpec` that has this subplotspec as
        a parent.

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

        ncols : int
            Number or columns in grid.

        Returns
        -------
        gridspec : `.GridSpec`

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

        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])
        )rz   )r   r   r   re   r   r   r   subgridspec  s    'zSubplotSpec.subgridspec)N)F)r   rI   rJ   rK   r   rY   rZ   rb   r   r   r~   rd   r   r   r   r   r   r   r   rE     s   


rE   )rK   ro   Zloggingrx   Znumpyr2   Z
matplotlibra   r   r   r   r   Zmatplotlib.transformsr   Zmatplotlib._layoutboxrQ   rR   Z	getLoggerr   Z_logr   r   rL   rz   rE   r   r   r   r   <module>   s   
  .=