B
    ]”t\U  ã               @   s²   d Z ddlZddlmZmZmZ ddlmZmZ ddlm	Z	 ddl
mZ ddlmZ dd	lmZ G d
d„ de	ƒZG dd„ deƒZG dd„ deƒZddd„Zejje e¡d dS )aG  
Place a table below the x-axis at location loc.

The table consists of a grid of cells.

The grid need not be rectangular and can have holes.

Cells are added by specifying their row and column.

For the purposes of positioning the cell at (0, 0) is
assumed to be at the top left and the cell at (max_row, max_col)
is assumed to be at bottom right.

You can add additional cells outside this range to have convenient
ways of positioning more interesting grids.

Author    : John Gill <jng@europe.renre.com>
Copyright : 2004 John Gill and John Hunter
License   : matplotlib license

é    Né   )ÚartistÚcbookÚ	docstring)ÚArtistÚallow_rasterization)Ú	Rectangle)ÚText)ÚBbox)ÚPathc               @   sz   e Zd 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„ Zedd„ ƒZdd„ Zdd„ Zdd„ Zdd„ ZdS )!ÚCellz>
    A cell is a  `.Rectangle` with some associated text.
    gš™™™™™¹?ÚkÚwTÚ Nc
       
      C   s\   t j| |||||d |  d¡ |d kr,d}|| _t|d |d ||	d| _| j d¡ d S )N)ÚwidthÚheightÚ	edgecolorÚ	facecolorFÚrightr   r   )ÚxÚyÚtextÚfontproperties)r   Ú__init__Úset_clip_onÚ_locr	   Ú_text)
ÚselfÚxyr   r   r   r   Zfillr   Úlocr   © r    ú/lib/python3.7/site-packages/matplotlib/table.pyr   &   s    	


zCell.__init__c             C   s   t  | |¡ d| _d S )NT)r   Úset_transformÚstale)r   Ztransr    r    r!   r"   ;   s    zCell.set_transformc             C   s   t  | |¡ | j |¡ d S )N)r   Ú
set_figurer   )r   Zfigr    r    r!   r$   @   s    zCell.set_figurec             C   s   | j S )z!Return the cell `.Text` instance.)r   )r   r    r    r!   Úget_textD   s    zCell.get_textc             C   s   | j  |¡ d| _d S )NT)r   Úset_fontsizer#   )r   Úsizer    r    r!   r&   H   s    zCell.set_fontsizec             C   s
   | j  ¡ S )zReturn the cell fontsize.)r   Úget_fontsize)r   r    r    r!   r(   L   s    zCell.get_fontsizec             C   sL   |   ¡ }|  |¡}x4|dkrF||  ¡ krF|d8 }|  |¡ |  |¡}qW |S )z# Shrink font size until text fits. r   )r(   Úget_required_widthÚ	get_widthr&   )r   ÚrendererÚfontsizeZrequiredr    r    r!   Úauto_set_font_sizeP   s    

zCell.auto_set_font_sizec             C   s8   |   ¡ sd S t | |¡ |  |¡ | j |¡ d| _d S )NF)Úget_visibler   ÚdrawÚ_set_text_positionr   r#   )r   r+   r    r    r!   r/   [   s    
z	Cell.drawc       	      C   s¬   |   |¡}|j\}}}}| j d¡ ||d  }| jdkrT| j d¡ ||d  }nD| jdkrz| j d¡ ||| j  }n| j d¡ ||d| j   }| j ||f¡ dS )zm Set text up so it draws in the right place.

        Currently support 'left', 'center' and 'right'
        Úcenterg       @Úleftr   g      ð?N)Úget_window_extentÚboundsr   Zset_verticalalignmentr   Zset_horizontalalignmentÚPADZset_position)	r   r+   ÚbboxÚlÚbr   Úhr   r   r    r    r!   r0   g   s    


zCell._set_text_positionc             C   s    | j  |¡}| |  ¡ ¡}|jS )z' Get text bounds in axes co-ordinates. )r   r3   Úinverse_transformedZget_data_transformr4   )r   r+   r6   Zbboxar    r    r!   Úget_text_bounds€   s    zCell.get_text_boundsc             C   s$   |   |¡\}}}}|dd| j   S )z# Get width required for this cell. g      ð?g       @)r;   r5   )r   r+   r7   r8   r   r9   r    r    r!   r)   †   s    zCell.get_required_widthc             K   s   | j  |¡ d| _dS )z&update the text properties with kwargsTN)r   Úupdater#   )r   Úkwargsr    r    r!   Úset_text_props‹   s    zCell.set_text_props)r   r   Tr   NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r5   r   r"   r$   r%   r&   r(   r-   r   r/   r0   r;   r)   r>   r    r    r    r!   r       s$       
r   c                   sT   e Zd ZdZdZdedddœZ‡ fdd„Zed	d
„ ƒZej	dd
„ ƒZdd„ Z
‡  ZS )Ú
CustomCellzD
    A subclass of Cell where the sides may be visibly toggled.
    ZBRTLr   ZBTZRL)ÚopenÚclosedZ
horizontalZverticalc               s   t ƒ j||Ž || _d S )N)Úsuperr   Úvisible_edges)r   rG   Úargsr=   )Ú	__class__r    r!   r      s    zCustomCell.__init__c             C   s   | j S )N)Ú_visible_edges)r   r    r    r!   rG   ¡   s    zCustomCell.visible_edgesc          	   C   st   |d kr| j | _nX|| jkr*| j| | _n@x8|D ]0}|| j kr0td |d | j¡d | j ¡¡ƒ‚q0W || _d| _d S )Nz=Invalid edge param {}, must only be one of {} or string of {}z, T)Ú_edgesrJ   Ú_edge_aliasesÚ
ValueErrorÚformatÚjoinr#   )r   ÚvalueÚedger    r    r!   rG   ¥   s    




c             C   s„   t jg}x2| jD ](}|| jkr,| t j¡ q| t j¡ qW t j|dd… krXt j|d< t ddgddgddgddgddgg|ddS )zV
        Return a path where the edges specified by _visible_edges are drawn.
        r   Néÿÿÿÿg        g      ð?T)Úreadonly)r   ZMOVETOrK   rJ   ÚappendZLINETOZ	CLOSEPOLY)r   ÚcodesrQ   r    r    r!   Úget_path¶   s    

 zCustomCell.get_path)r?   r@   rA   rB   rK   rL   r   ÚpropertyrG   ÚsetterrV   Ú__classcell__r    r    )rI   r!   rC   ‘   s   rC   c               @   s  e Zd ZdZdddddddd	d
ddddddddddœZdZdZdDdd„Zdd„ Zdd„ Z	dd„ Z
edd „ ƒZejd!d „ ƒZd"d#„ Zed$d%„ ƒZd&d'„ Zd(d)„ Zd*d+„ Ze d,¡eƒZd-d.„ Zd/d0„ Zd1d2„ Zd3d4„ ZdEd6d7„Zd8d9„ Zd:d;„ Zd<d=„ Zd>d?„ Zd@dA„ Z dBdC„ Z!dS )FÚTablea5  
    Create a table of cells.

    Table can have (optional) row and column headers.

    Each entry in the table can be either text or patches.

    Column widths and row heights for the table can be specified.

    Return value is a sequence of text, line and patch instances that make
    up the table
    r   r   é   é   é   é   é   é   é   é	   é
   é   é   é   é   é   é   é   )Zbestzupper rightz
upper leftz
lower leftzlower rightzcenter leftzcenter rightzlower centerzupper centerr1   z	top rightztop leftzbottom leftzbottom rightr   r2   ÚtopÚbottomg{®Gáz”?Nc             K   s¬   t  | ¡ t|tƒrF|| jkr<t d|d | j¡f ¡ d}| j| }|  |j	¡ || _
|| _|| _|  |j¡ g | _i | _d | _g | _g | _d| _|  |¡ |  d¡ d S )NzIUnrecognized location %s. Falling back on bottom; valid locations are
%s	z
	rl   TF)r   r   Ú
isinstanceÚstrrU   ÚwarningsÚwarnrO   r$   ÚfigureÚ_axesr   Ú_bboxr"   Z	transAxesZ_textsÚ_cellsrK   Z	_autoRowsÚ_autoColumnsÚ_autoFontsizer<   r   )r   Úaxr   r6   r=   r    r    r!   r   ð   s(    




zTable.__init__c             O   s.   d}t |f|žd| ji|—Ž}|| ||f< |S )zù
        Add a cell to the table.

        Parameters
        ----------
        row : int
            Row index.
        col : int
            Column index.

        Returns
        -------
        `CustomCell`: Automatically created cell

        )r   r   rG   )rC   Úedges)r   ÚrowÚcolrH   r=   r   Úcellr    r    r!   Úadd_cell  s    zTable.add_cellc             C   s‚   t |tƒstdƒ‚y|d |d  }}W n tk
rD   tdƒ‚Y nX | | j¡ | |  ¡ ¡ | 	d¡ || j
||f< d| _dS )z8
        Set a custom cell in a given position.
        zTable only accepts CustomCellr   r   z0Only tuples length 2 are accepted as coordinatesFTN)rm   rC   Ú	TypeErrorÚ	ExceptionÚKeyErrorr$   rq   r"   Úget_transformr   rt   r#   )r   Úpositionr{   ry   rz   r    r    r!   Ú__setitem__"  s    

zTable.__setitem__c             C   sB   y|d |d  }}W n t k
r2   tdƒ‚Y nX | j||f S )z?
        Retrieve a custom cell from a given position.
        r   r   z0Only tuples length 2 are accepted as coordinates)r~   r   rt   )r   r   ry   rz   r    r    r!   Ú__getitem__2  s
    zTable.__getitem__c             C   s   | j S )N)rK   )r   r    r    r!   rx   <  s    zTable.edgesc             C   s   || _ d| _d S )NT)rK   r#   )r   rP   r    r    r!   rx   @  s    c             C   s    | j d | jj | jjj d S )Ng      R@g333333ó?)ÚFONTSIZErq   Zdpirr   r6   r   )r   r    r    r!   Ú_approx_text_heightE  s    zTable._approx_text_heightc             C   sx   |d kr| j j}|d kr tdƒ‚|  ¡ s,d S | d¡ |  |¡ x"t| jƒD ]}| j|  |¡ qLW | 	d¡ d| _
d S )NzNo renderer definedÚtableF)rq   Ú_cachedRendererÚRuntimeErrorr.   Z
open_groupÚ_update_positionsÚsortedrt   r/   Zclose_groupr#   )r   r+   Úkeyr    r    r!   r/   I  s    


z
Table.drawc                s0   ‡ fdd„| j  ¡ D ƒ}t |¡}| |  ¡ ¡S )zrGet a bbox, in axes co-ordinates for the cells.

        Only include those in the range (0,0) to (maxRow, maxCol)c                s.   g | ]&\\}}}|d kr|d kr|  ˆ ¡‘qS )r   )r3   )Ú.0ry   rz   r{   )r+   r    r!   ú
<listcomp>a  s   
z(Table._get_grid_bbox.<locals>.<listcomp>)rt   Úitemsr
   Úunionr:   r€   )r   r+   Úboxesr6   r    )r+   r!   Ú_get_grid_bbox]  s    

zTable._get_grid_bboxc                sh   t | jƒr|  | |¡S | jj‰ ˆ dk	r\‡ fdd„| j ¡ D ƒ}t |¡}| |j	|j
¡i fS di fS dS )zUTest whether the mouse event occurred in the table.

        Returns T/F, {}
        Nc                s.   g | ]&\\}}}|d kr|d kr|  ˆ ¡‘qS )r   )r3   )rŒ   ry   rz   r{   )r+   r    r!   r   s  s   
z"Table.contains.<locals>.<listcomp>F)ÚcallableZ	_containsrq   r‡   rt   rŽ   r
   r   Úcontainsr   r   )r   Z
mouseeventr   r6   r    )r+   r!   r“   g  s    


zTable.containsc             C   s   t | j ¡ ƒS )z*Return the Artists contained by the table.)Úlistrt   Úvalues)r   r    r    r!   Úget_children{  s    zTable.get_childrenz3.0c                s"   ‡ fdd„| j  ¡ D ƒ}t |¡S )z6Return the bounding box of the table in window coords.c                s   g | ]}|  ˆ ¡‘qS r    )r3   )rŒ   r{   )r+   r    r!   r   ‚  s   z+Table.get_window_extent.<locals>.<listcomp>)rt   r•   r
   r   )r   r+   r   r    )r+   r!   r3   €  s    
zTable.get_window_extentc             C   sü   i }i }xV| j  ¡ D ]H\\}}}| |d¡}t|| ¡ ƒ||< | |d¡}t|| ¡ ƒ||< qW d}i }	x$t|ƒD ]}||	|< ||| 7 }qrW d}
i }x(t|ddD ]}|
||< |
|| 7 }
q¤W x6| j  ¡ D ](\\}}}| |	| ¡ | || ¡ qÌW dS )zW Calculate row heights and column widths.

        Position cells accordingly.
        g        r   T)ÚreverseN)	rt   rŽ   Ú
setdefaultÚmaxÚ
get_heightr*   rŠ   Úset_xÚset_y)r   ZwidthsZheightsry   rz   r{   r   r   ZxposZleftsZyposZbottomsr    r    r!   Ú_do_cell_alignment†  s(    zTable._do_cell_alignmentc          	   C   sV   yt |ƒ W n$ ttfk
r0   | j |¡ Y nX x|D ]}| j |¡ q8W d| _dS )aÕ   Given column indexs in either List, Tuple or int. Will be able to
        automatically set the columns into optimal sizes.

        Here is the example of the input, which triger automatic adjustment on
        columns to optimal size by given index numbers.
        -1: the row labling
        0: the 1st column
        1: the 2nd column

        Args:
            col(List): list of indexs
            >>>table.auto_set_column_width([-1,0,1])

            col(Tuple): tuple of indexs
            >>>table.auto_set_column_width((-1,0,1))

            col(int): index integer
            >>>table.auto_set_column_width(-1)
            >>>table.auto_set_column_width(0)
            >>>table.auto_set_column_width(1)
        TN)Úiterr}   ÚAttributeErrorru   rT   r#   )r   rz   r{   r    r    r!   Úauto_set_column_width¦  s    
zTable.auto_set_column_widthc                sb   ‡ fdd„| j D ƒ}d}x&|D ]}| j | }t| |¡|ƒ}qW x|D ]}| j |  |¡ qFW dS )z#Automatically set width for column.c                s   g | ]}|d  ˆ kr|‘qS )r   r    )rŒ   r‹   )rz   r    r!   r   É  s    z0Table._auto_set_column_width.<locals>.<listcomp>r   N)rt   r™   r)   Ú	set_width)r   rz   r+   Úcellsr   r{   Úcr    )rz   r!   Ú_auto_set_column_widthÇ  s    


zTable._auto_set_column_widthTc             C   s   || _ d| _dS )z Automatically set font size. TN)rv   r#   )r   rP   r    r    r!   r-   Õ  s    zTable.auto_set_font_sizec             C   s”   t | jƒdkrd S tt| j ¡ ƒƒ ¡ }g }xD| j ¡ D ]6\}}|d | jkrPq8| |¡}t	||ƒ}| 
|¡ q8W x| j ¡ D ]}| |¡ q~W d S )Nr   r   )Úlenrt   Únextrž   r•   r(   rŽ   ru   r-   ÚminrT   r&   )r   r+   r,   r¢   r‹   r{   r'   r    r    r!   Ú_auto_set_font_sizeÚ  s    

zTable._auto_set_font_sizec             C   s<   x6| j  ¡ D ](}| | ¡ | ¡ | | ¡ | ¡ qW dS )z: Scale column widths by xscale and row heights by yscale. N)rt   r•   r¡   r*   Z
set_heightrš   )r   ZxscaleZyscaler£   r    r    r!   Úscaleì  s    zTable.scalec             C   s(   x| j  ¡ D ]}| |¡ qW d| _dS )z}
        Set the font size, in points, of the cell text.

        Parameters
        ----------
        size : float
        TN)rt   r•   r&   r#   )r   r'   r{   r    r    r!   r&   ò  s    	zTable.set_fontsizec             C   sF   x@| j  ¡ D ]2}| ¡ | ¡  }}| || ¡ | || ¡ qW dS )z-Move all the artists by ox, oy (axes coords).N)rt   r•   Zget_xZget_yr›   rœ   )r   ÚoxÚoyr£   r   r   r    r    r!   Ú_offsetÿ  s    zTable._offsetc              C   s,  x| j D ]}|  ||¡ qW | jr,|  |¡ |  ¡  |  |¡}|j\}}}}| jd k	r”| j\}}	}
}|  |
| || ¡ || }|	| }|  ¡  nˆt	t
| jƒƒ\}}}}}}}}}}}}}}}}}}d|d  | }d|d  | }| j|||fkr| j| }| j|||||fkr*d|| | j  }| j||||fkrPd|| | j  }| j|||fkrl| j| }| j|||fkrŽd|d  | }| j|||fkr°d|d  | }| j|||fkrÌ||  }| j|||fkræd| }| j|||fkr d| }| j|||fkr||  }|  ||¡ d S )Ng      à?r[   r   g      ð?)ru   r¤   rv   r¨   r   r‘   r4   rs   r©   Úranger¥   rU   r   ÚAXESPADr¬   ) r   r+   rz   r6   r7   r8   r   r9   ZrlÚrbZrwZrhrª   r«   ZBESTZURZULZLLZLRZCLZCRZLCZUCÚCZTRZTLZBLZBRÚRÚLÚTÚBr    r    r!   r‰     sJ    


2



zTable._update_positionsc             C   s   | j S )z$Return a dict of cells in the table.)rt   )r   r    r    r!   Ú	get_celld=  s    zTable.get_celld)NN)T)"r?   r@   rA   rB   rU   r„   r®   r   r|   r‚   rƒ   rW   rx   rX   r…   r   r/   r‘   r“   r–   r   Z
deprecatedZget_child_artistsr3   r   r    r¤   r-   r¨   r©   r&   r¬   r‰   rµ   r    r    r    r!   rZ   Ì   sX   


 !
7rZ   r   r2   r1   rl   rE   c             K   sÊ  |dkr|dkrt dƒ‚|dkrDt|ƒ}t|d ƒ}dg| g| }t|ƒ}t|d ƒ}x&|D ]}t|ƒ|kr^t d |¡ƒ‚q^W |dk	rÌt|ƒ|kr¢t d |¡ƒ‚x6|D ]}t|ƒ|kr¨t d |¡ƒ‚q¨W nd| g| }|dkrðd	| g| }d}|dkr|dk	r.dg| }|d }n|dkr.d| }|dk	rTt|ƒ|krTt d
 |¡ƒ‚d}|dkr~|	dk	rxdg| }nd}n|	dkrd| }	|dkr¨d| g| }t| ||f|Ž}||_| ¡ }xVt|ƒD ]J}xBt|ƒD ]6}|j|| ||| ||| | || | |d qÞW qÐW |dk	r`x6t|ƒD ]*}|jd||| ||| |	| |
d q2W |dk	r¼x<t|ƒD ]0}|j|| d|pŒd||| || |d qtW |dkr¼| d¡ |  	|¡ |S )at  
    TABLE(cellText=None, cellColours=None,
          cellLoc='right', colWidths=None,
          rowLabels=None, rowColours=None, rowLoc='left',
          colLabels=None, colColours=None, colLoc='center',
          loc='bottom', bbox=None, edges='closed')

    Factory function to generate a Table instance.

    Thanks to John Gill for providing the class and table.
    NzZAt least one argument from "cellColours" or "cellText" must be provided to create a table.r   r   z+Each row in 'cellText' must have {} columnsz'cellColours' must have {} rowsz.Each row in 'cellColours' must have {} columnsr   g      ð?z!'rowLabels' must be of length {0}r   )r   r   r   r   r   rR   gVçž¯Ò<)
rM   r¥   rN   rZ   rx   r…   r­   r|   r    Z	add_table)rw   ZcellTextZcellColoursZcellLocZ	colWidthsZ	rowLabelsZ
rowColoursZrowLocZ	colLabelsZ
colColoursZcolLocr   r6   rx   r=   ZrowsZcolsry   ZrowLabelWidthÚoffsetr†   r   rz   r    r    r!   r†   B  s†    



















r†   )rZ   )NNr   NNNr2   NNr1   rl   NrE   )rB   ro   r   r   r   r   r   r   Zpatchesr   r   r	   Z
transformsr
   Úpathr   r   rC   rZ   r†   Zinterpdr<   Zkwdocr    r    r    r!   Ú<module>   s$   q;  y    
n