B
    ]”t\%  ã               @   s¾   d dl Z d dlZd dlmZ d dlmZ d dlmZ d dl	m
Z
mZ d dlmZ G dd„ deƒZi Ze  d¡ddd„ƒZeƒ Zd	d
„ Zejje e¡d e ej¡ ejje e¡d dS )é    N)Ú	docstring)ÚAxes)ÚGridSpecÚSubplotSpecc               @   s€   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚSubplotBasez¶
    Base class for subplots, which are :class:`Axes` instances with
    additional methods to facilitate generating and manipulating a set
    of :class:`Axes` within a figure.
    c       	      O   sè  || _ t|ƒdkrŠt|d tƒr,|d | _n\y$tt|d ƒƒ}tt|ƒ\}}}W n tk
rl   tdƒ‚Y nX t	||| j d|d  | _nÐt|ƒdkrL|\}}}t|ƒ}t|ƒ}t|t
ƒrþt|ƒdkrþdd„ |D ƒ}t	||| j d|d d |d … | _nL|dk s||| kr,td	j|| |d
ƒ‚t	||| j dt|ƒd  | _ntd|f ƒ‚|  ¡  | jj| || jf|Ž | jjdkr”d| _d| _nP| jjjd }|t ¡  }tj| jj|| d| _tj| j| jjd dd| d| _dS )a  
        *fig* is a :class:`matplotlib.figure.Figure` instance.

        *args* is the tuple (*numRows*, *numCols*, *plotNum*), where
        the array of subplots in the figure has dimensions *numRows*,
        *numCols*, and where *plotNum* is the number of the subplot
        being created.  *plotNum* starts at 1 in the upper left
        corner and increases to the right.

        If *numRows* <= *numCols* <= *plotNum* < 10, *args* can be the
        decimal integer *numRows* * 100 + *numCols* * 10 + *plotNum*.
        é   r   z4Single argument to subplot must be a 3-digit integer)Úfigureé   é   c             S   s   g | ]}t |ƒ‘qS © )Úint)Ú.0Únr   r   ú8lib/python3.7/site-packages/matplotlib/axes/_subplots.pyú
<listcomp>4   s    z(SubplotBase.__init__.<locals>.<listcomp>z)num must be 1 <= num <= {maxn}, not {num})ZmaxnÚnumz"Illegal argument(s) to subplot: %sNz.ax)ÚparentÚnameÚartistz.posT)r   r   ÚposZsubplotr   )r   ÚlenÚ
isinstancer   Ú_subplotspecÚstrr   ÚmapÚ
ValueErrorr   ÚtupleÚformatÚupdate_paramsÚ_axes_classÚ__init__ÚfigboxÚ
_layoutboxÚ_poslayoutboxr   Ú	layoutboxZseq_idZ	LayoutBox)	ÚselfZfigÚargsÚkwargsÚsÚrowsÚcolsr   r   r   r   r   r       sT    
"

zSubplotBase.__init__c             C   s(   t dd„ t| ƒjD ƒƒ}t|f|  ¡ fS )Nc             s   s&   | ]}t |tƒrt |tƒs|V  qd S )N)Ú
issubclassr   r   )r   Úcr   r   r   ú	<genexpr>\   s    z)SubplotBase.__reduce__.<locals>.<genexpr>)ÚnextÚtypeÚ__mro__Ú$_picklable_subplot_class_constructorÚ__getstate__)r%   Ú
axes_classr   r   r   Ú
__reduce__Y   s
    zSubplotBase.__reduce__c             C   s"   |   ¡  ¡ \}}}}|||d fS )z%get the subplot geometry, e.g., 2,2,3r   )Úget_subplotspecÚget_geometry)r%   r)   r*   Znum1Znum2r   r   r   r6   b   s    zSubplotBase.get_geometryc             C   s2   t ||| jd|d  | _|  ¡  |  | j¡ dS )z2change subplot geometry, e.g., from 1,1,1 to 2,2,3)r   r   N)r   r   r   r   Zset_positionr!   )r%   ZnumrowsZnumcolsr   r   r   r   Úchange_geometryh   s    zSubplotBase.change_geometryc             C   s   | j S )z8get the SubplotSpec instance associated with the subplot)r   )r%   r   r   r   r5   o   s    zSubplotBase.get_subplotspecc             C   s
   || _ dS )z8set the SubplotSpec instance associated with the subplotN)r   )r%   Zsubplotspecr   r   r   Úset_subplotspecs   s    zSubplotBase.set_subplotspecc             C   s
   | j  ¡ S )z5get the GridSpec instance associated with the subplot)r   Úget_gridspec)r%   r   r   r   r9   w   s    zSubplotBase.get_gridspecc             C   s,   |   ¡ j| jdd\| _| _| _| _| _dS )z0update the subplot position from fig.subplotparsT)Z
return_allN)r5   Zget_positionr   r!   ÚrowNumÚcolNumÚnumRowsÚnumCols)r%   r   r   r   r   {   s    zSubplotBase.update_paramsc             C   s
   | j dkS )Nr   )r;   )r%   r   r   r   Úis_first_col‚   s    zSubplotBase.is_first_colc             C   s
   | j dkS )Nr   )r:   )r%   r   r   r   Úis_first_row…   s    zSubplotBase.is_first_rowc             C   s   | j | jd kS )Nr   )r:   r<   )r%   r   r   r   Úis_last_rowˆ   s    zSubplotBase.is_last_rowc             C   s   | j | jd kS )Nr   )r;   r=   )r%   r   r   r   Úis_last_col‹   s    zSubplotBase.is_last_colc             C   s”   |   ¡ }|  ¡ }|sPx| jddD ]}| d¡ q"W |  ¡  ¡  d¡ |  d¡ |sx| jddD ]}| d¡ qbW |  ¡  ¡  d¡ |  	d¡ dS )z¨Only show "outer" labels and tick labels.

        x-labels are only kept for subplots on the last row; y-labels only for
        subplots on the first column.
        Zboth)ZwhichFÚ N)
r@   r>   Zget_xticklabelsZset_visibleZ	get_xaxisZget_offset_textZ
set_xlabelZget_yticklabelsZ	get_yaxisZ
set_ylabel)r%   ZlastrowZfirstcolZlabelr   r   r   Úlabel_outer   s    
zSubplotBase.label_outerc             O   sÚ   ddl m} d|kr<d|kr<|d | k	r<|d | k	r<tdƒ‚|  ¡ f| }|| jf|ž|Ž\}}}t|ƒ| jf|ž|Ž}| j |¡ |  d¡ | d¡ | jdk	rÈ|jdk	rÈ|j 	| j¡ |j
 	| j
¡ | j | |¡ |S )zN
        Make a twinx axes of self. This is used for twinx and twiny.
        r   )Úprocess_projection_requirementsZsharexZshareyz%Twinned Axes may share only one axis.ZdatalimN)Zmatplotlib.projectionsrD   r   r5   r   Úsubplot_class_factoryZadd_subplotZset_adjustabler"   Zconstrain_samer#   Z_twinned_axesÚjoin)r%   Zklr'   rD   Zprojection_classÚkeyZax2r   r   r   Ú_make_twin_axes¢   s"    


zSubplotBase._make_twin_axesN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r    r4   r6   r7   r5   r8   r9   r   r>   r?   r@   rA   rC   rH   r   r   r   r   r      s   G	r   c                sV   ˆ dkrt ‰ yt‡ fdd„t ¡ D ƒƒS  tk
rP   tdˆ j tˆ fdˆ iƒS X dS )aL  
    This makes a new class that inherits from `.SubplotBase` and the
    given axes_class (which is assumed to be a subclass of `.axes.Axes`).
    This is perhaps a little bit roundabout to make a new class on
    the fly like this, but it means that a new Subplot class does
    not have to be created for every type of Axes.
    Nc             3   s    | ]}|j tˆ fkr|V  qd S )N)Ú	__bases__r   )r   Úcls)r3   r   r   r-   Ú   s    z(subplot_class_factory.<locals>.<genexpr>z	%sSubplotr   )r   r.   r   Ú__subclasses__ÚStopIterationr/   rI   )r3   r   )r3   r   rE   Ë   s    	
rE   c             C   s   t | ƒ}| |¡S )z£
    This stub class exists to return the appropriate subplot class when called
    with an axes class. This is purely to allow pickling of Axes and Subplots.
    )rE   Ú__new__)r3   Zsubplot_classr   r   r   r1   æ   s    r1   )ÚSubplot)N)Ú	functoolsÚwarningsZ
matplotlibr   Zmatplotlib.artistr   ZmartistZmatplotlib.axes._axesr   Zmatplotlib.gridspecr   r   Zmatplotlib._layoutboxr"   r$   Úobjectr   Z_subplot_classesÚ	lru_cacherE   rR   r1   ZinterpdÚupdateZkwdocZdedent_interpdr    r   r   r   r   Ú<module>   s     >	