3
qdCI                 @   s   d dl Z d dlZd dlZd dlmZ d dlmZ d dljj	Z
d dlmZ d dlZd dlmZmZ d dljZe
jG dd dZdd	d
Zdd ZdS )    N)random)cache_readonly)is_list_like)	DataFrameSeriesc               @   s   e Zd Zdd Zdd Zedd Zedd Zd.ddZd/ddZ	dd Z
d0ddZdd Zd1ddZdd Zd2ddZd3ddZd4ddZd d! Zd"d# Zd5d%d&Zd6d'd(Zi fd)d*Zd7d,d-Zd	S )8TestPlotBasec             C   s   dd l }ddlm} |j  |j | _|j | _|j | _	|j
 | _d| _d| _d| _d| _d}tjd	h tjjd
dg|d}tjjdddg|d}t||tjdd|dtjdd|dtjd|dd| _W d Q R X tj | _ttjjddtjjddtjdtjjdd d| _d S )Nr   )compat      皙@333333@leftd   *   ZMaleZFemale)sizeABCB             )gender	classroomheightZweightcategory   )r   r   r   )r   r   )
matplotlibZpandas.plotting._matplotlibr   Z
rcdefaultsZ_mpl_ge_2_2_3Zmpl_ge_2_2_3Z_mpl_ge_3_0_0Zmpl_ge_3_0_0Z_mpl_ge_3_1_0mpl_ge_3_1_0Z_mpl_ge_3_2_0Zmpl_ge_3_2_0Zbp_n_objectsZpolycollection_factordefault_figsizeZdefault_tick_positiontmZ
RNGContextnpr   choicer   ZnormalrandintZhist_dfZmakeTimeDataFrameZtdfuniformZarangeZ	hexbin_df)selfmethodmplr   nr   r    r)   >/tmp/pip-build-7vycvbft/pandas/pandas/tests/plotting/common.pysetup_method   s4    



 
zTestPlotBase.setup_methodc             C   s   t j  d S )N)r    close)r%   r&   r)   r)   r*   teardown_methodA   s    zTestPlotBase.teardown_methodc             C   s   dd l j}|S )Nr   )matplotlib.pyplotpyplot)r%   pltr)   r)   r*   r0   D   s    
zTestPlotBase.pltc             C   s   dd l j}|jS )Nr   )Zmatplotlib.colorscolorsZcolorConverter)r%   r1   r)   r)   r*   colorconverterJ   s    
zTestPlotBase.colorconverterNTc             C   sj   |r|dkrt d| j|}xF|D ]>}|rR|j dk	s<t| j|j j | q$|j dks$tq$W dS )aQ  
        Check each axes has expected legend labels

        Parameters
        ----------
        axes : matplotlib Axes object, or its list-like
        labels : list-like
            expected legend labels
        visible : bool
            expected legend visibility. labels are checked only when visible is
            True
        Nz-labels must be specified when visible is True)
ValueError_flatten_visible
get_legendAssertionError_check_text_labelsZ	get_texts)r%   axeslabelsvisibleaxr)   r)   r*   _check_legend_labelsP   s    

z!TestPlotBase._check_legend_labelsc             C   sT   |r|dkrt d|r@|j \}}dd |D }||ksPtn|j dksPtdS )aB  
        Check ax has expected legend markers

        Parameters
        ----------
        ax : matplotlib Axes object
        expected_markers : list-like
            expected legend markers
        visible : bool
            expected legend visibility. labels are checked only when visible is
            True
        Nz.Markers must be specified when visible is Truec             S   s   g | ]}|j  qS r)   )Z
get_marker).0handler)   r)   r*   
<listcomp>x   s    z5TestPlotBase._check_legend_marker.<locals>.<listcomp>)r3   Zget_legend_handles_labelsr6   r5   )r%   r;   Zexpected_markersr:   Zhandles_markersr)   r)   r*   _check_legend_markerg   s    z!TestPlotBase._check_legend_markerc                sP   |j  }|j  }dd  t|t|ks,t fddt||D  tj  dS )z
        Check each axes has identical lines

        Parameters
        ----------
        xp : matplotlib Axes object
        rs : matplotlib Axes object
        c             S   s    | j  }|j  }tj|| d S )N)Z
get_xydatar    assert_almost_equal)xplrslZxpdataZrsdatar)   r)   r*   
check_line   s    z,TestPlotBase._check_data.<locals>.check_linec                s   g | ]\}} ||qS r)   r)   )r=   rD   rE   )rF   r)   r*   r?      s    z,TestPlotBase._check_data.<locals>.<listcomp>N)	get_lineslenr6   zipr    r,   )r%   ZxprsZxp_linesZrs_linesr)   )rF   r*   _check_data}   s    	zTestPlotBase._check_datac             C   sJ   ddl m} t|| r(t| r(|g}x|D ]}|j |ks.tq.W dS )a   
        Check each artist is visible or not

        Parameters
        ----------
        collections : matplotlib Artist or its list-like
            target Artist or its list or collection
        visible : bool
            expected visibility
        r   )
CollectionN)matplotlib.collectionsrL   
isinstancer   get_visibler6   )r%   collectionsr:   rL   patchr)   r)   r*   _check_visible   s
    
zTestPlotBase._check_visiblec                s*   |j  }tt||  fdd|jD S )Nc                s   g | ]} | qS r)   r)   )r=   v)mappedr)   r*   r?      s    z3TestPlotBase._get_colors_mapped.<locals>.<listcomp>)uniquedictrI   values)r%   Zseriesr1   rU   r)   )rT   r*   _get_colors_mapped   s    zTestPlotBase._get_colors_mappedc             C   s  ddl m}m}m} ddlm} | j}	|dk	r|dk	rR| j||}|dt| }t|t|ksft	xrt
||D ]d\}
}t|
|r|
j }|	j|}n(t|
||frt|
j d }n|
j }|	j|}||ksrt	qrW |dk	r|dk	r
| j||}|dt| }t|t|ks t	xht
||D ]Z\}
}t|
|rN|
j d }n|
j }t|tjrlt|}|	j|}||ks,t	q,W dS )a3  
        Check each artist has expected line colors and face colors

        Parameters
        ----------
        collections : list-like
            list or collection of target artist
        linecolors : list-like which has the same length as collections
            list of expected line colors
        facecolors : list-like which has the same length as collections
            list of expected face colors
        mapping : Series
            Series used for color grouping key
            used for andrew_curves, parallel_coordinates, radviz test
        r   )rL   LineCollectionPolyCollection)Line2DN)rM   rL   rY   rZ   Zmatplotlib.linesr[   r2   rX   rH   r6   rI   rN   Z	get_colorZto_rgbatupleZget_edgecolorZget_facecolorr!   Zndarray)r%   rP   Z
linecolorsZ
facecolorsmappingrL   rY   rZ   r[   convrQ   colorresultexpectedr)   r)   r*   _check_colors   s<    




zTestPlotBase._check_colorsc             C   sd   t |s|j |ks`tnFdd |D }t|t|ks<tx"t||D ]\}}||ksHtqHW dS )a.  
        Check each text has expected labels

        Parameters
        ----------
        texts : matplotlib Text object, or its list-like
            target text, or its list
        expected : str or list-like which has the same length as texts
            expected text label, or its list
        c             S   s   g | ]}|j  qS r)   )get_text)r=   tr)   r)   r*   r?      s    z3TestPlotBase._check_text_labels.<locals>.<listcomp>N)r   rc   r6   rH   rI   )r%   Ztextsra   r9   labeler)   r)   r*   r7      s    zTestPlotBase._check_text_labelsc       
      C   s&  ddl m} | j|}x|D  ]}|dk	s4|dk	rt|jj |rN|j }n|j |jdd }x<|D ]4}	|dk	rtj|	j	 | |dk	rhtj|	j
 | qhW |dk	s|dk	rt|jj |r|j }n|j |jdd }x>|D ]6}	|dk	rtj|	j	 | |dk	rtj|	j
 | qW qW dS )a  
        Check each axes has expected tick properties

        Parameters
        ----------
        axes : matplotlib Axes object, or its list-like
        xlabelsize : number
            expected xticks font size
        xrot : number
            expected xticks rotation
        ylabelsize : number
            expected yticks font size
        yrot : number
            expected yticks rotation
        r   )NullFormatterNT)minor)Zmatplotlib.tickerrg   r4   rN   xaxisZget_minor_formatterZget_xticklabelsr    rC   Zget_fontsizeZget_rotationyaxisZget_yticklabels)
r%   r8   Z
xlabelsizeZxrotZ
ylabelsizeZyrotrg   r;   r9   re   r)   r)   r*   _check_ticks_props   s*    





zTestPlotBase._check_ticks_propslinearc             C   s@   | j |}x0|D ](}|jj |ks&t|jj |kstqW dS )a  
        Check each axes has expected scales

        Parameters
        ----------
        axes : matplotlib Axes object, or its list-like
        xaxis : {'linear', 'log'}
            expected xaxis scale
        yaxis : {'linear', 'log'}
            expected yaxis scale
        N)r4   ri   Z	get_scaler6   rj   )r%   r8   ri   rj   r;   r)   r)   r*   _check_ax_scales-  s    

zTestPlotBase._check_ax_scalesc       	      C   s   ddl m} |dkr| j}| j|}|dk	r^t||ks<tx |D ]}t|j dksBtqBW |dk	r| j||}||ksttj	|d j
j tj|tjd dS )a  
        Check expected number of axes is drawn in expected layout

        Parameters
        ----------
        axes : matplotlib Axes object, or its list-like
        axes_num : number
            expected number of axes. Unnecessary axes should be set to
            invisible.
        layout : tuple
            expected layout, (expected number of rows , columns)
        figsize : tuple
            expected figsize. default is matplotlib default
        r   )_flattenN)Zdtype)!pandas.plotting._matplotlib.toolsrn   r   r4   rH   r6   Zget_children_get_axes_layoutr    Zassert_numpy_array_equalfigureZget_size_inchesr!   arrayZfloat64)	r%   r8   Zaxes_numZlayoutZfigsizern   Zvisible_axesr;   r`   r)   r)   r*   _check_axes_shape>  s    

zTestPlotBase._check_axes_shapec             C   sZ   t  }t  }x<|D ]4}|j j }|j|d d  |j|d d  qW t|t|fS )Nr      )setget_positionZ
get_pointsaddrH   )r%   r8   Zx_setZy_setr;   Zpointsr)   r)   r*   rp   b  s    
zTestPlotBase._get_axes_layoutc             C   s&   ddl m} ||}dd |D }|S )z
        Flatten axes, and filter only visible

        Parameters
        ----------
        axes : matplotlib Axes object, or its list-like

        r   )rn   c             S   s   g | ]}|j  r|qS r)   )rO   )r=   r;   r)   r)   r*   r?   x  s    z1TestPlotBase._flatten_visible.<locals>.<listcomp>)ro   rn   )r%   r8   rn   r)   r)   r*   r4   l  s    	zTestPlotBase._flatten_visibler   c             C   s   | j |}xp|D ]h}|j}d}d}x<|D ]4}t|dd}	t|dd}
|	rP|d7 }|
r(|d7 }q(W ||kslt||kstqW dS )a  
        Check axes has expected number of errorbars

        Parameters
        ----------
        axes : matplotlib Axes object, or its list-like
        xerr : number
            expected number of x errorbar
        yerr : number
            expected number of y errorbar
        r   has_xerrFhas_yerrrt   N)r4   
containersgetattrr6   )r%   r8   ZxerrZyerrr;   rz   Z
xerr_countZ
yerr_countcrx   ry   r)   r)   r*   _check_has_errorbars{  s    


z!TestPlotBase._check_has_errorbarsc             C   s  ddl m} t|td}|dkrj|dkr,d}t||| s>t|dkrft|j|sVtt|jtsftn$|dkrx | j|D ]}t||s~tq~W dS t|t	stt
|j t
|kstx|j D ]\}}	t|	|| st|dkr|r|	j |kstq|dkrP|r*|	jj |ks*tt|	j|s<tt|	jtstq|dkr|	d d }
|
j}|r|j |kstqtqW dS )	at  
        Check box returned type is correct

        Parameters
        ----------
        returned : object to be tested, returned from boxplot
        return_type : str
            return_type passed to boxplot
        expected_keys : list-like, optional
            group labels in subplot case. If not passed,
            the function checks assuming boxplot uses single ax
        check_ax_title : bool
            Whether to check the ax.title is the same as expected_key
            Intended to be checked by calling from ``boxplot``.
            Normal ``plot`` doesn't attach ``ax.title``, it must be disabled.
        r   )Axes)rV   r8   bothNrV   r   r8   Zmedians)Zmatplotlib.axesr~   rV   r\   rN   r6   r;   linesr4   r   sortedkeysitemsZ	get_titler8   )r%   Zreturnedreturn_typeZexpected_keysZcheck_ax_titler~   typesrkeyvalueliner8   r)   r)   r*   _check_box_return_type  s>    


z#TestPlotBase._check_box_return_typec                sp  dd l } fdd}d}xP|D ]F} jjddt| | |d7 }|jddd |jf d	|i| |  stt jjddt| | |d7 }|jdd
d |jf |dd| |  st|dkr  jjddt| | |d7 }|jdd
d |jf d	|i| | st jjddt| | |d7 }|jddd |jf |d
d| | s tq W d S )Nr   c                 sz    j j jj }  j j jj } jrLtdd | D }tdd |D }n$tdd | D }tdd |D }|ov| S )Nc             s   s   | ]}|j j  V  qd S )N)gridlinerO   )r=   gr)   r)   r*   	<genexpr>  s    zHTestPlotBase._check_grid_settings.<locals>.is_grid_on.<locals>.<genexpr>c             s   s   | ]}|j j  V  qd S )N)r   rO   )r=   r   r)   r)   r*   r     s    c             s   s   | ]}|j  V  qd S )N)gridOn)r=   r   r)   r)   r*   r     s    c             s   s   | ]}|j  V  qd S )N)r   )r=   r   r)   r)   r*   r     s    )r0   Zgcari   Zget_major_ticksrj   r   all)ZxticksZyticksZxoffZyoff)r%   r)   r*   
is_grid_on  s    z5TestPlotBase._check_grid_settings.<locals>.is_grid_onrt   r   r8   F)gridkindT)r   r   Zpie)r   r0   ZsubplotrH   rcZplotr6   )r%   objZkindskwsr'   r   Zspndxr   r)   )r%   r*   _check_grid_settings  s2    z!TestPlotBase._check_grid_settingsr_   c                s    fdd|d D S )zT
        Auxiliary function for correctly unpacking cycler after MPL >= 1.5
        c                s   g | ]}|  qS r)   r)   )r=   rS   )fieldr)   r*   r?     s    z/TestPlotBase._unpack_cycler.<locals>.<listcomp>zaxes.prop_cycler)   )r%   ZrcParamsr   r)   )r   r*   _unpack_cycler  s    zTestPlotBase._unpack_cycler)NT)NT)T)NNN)NNNN)rl   rl   )NNN)r   r   )NT)r_   )__name__
__module____qualname__r+   r-   r   r0   r2   r<   rB   rK   rR   rX   rb   r7   rk   rm   rs   rp   r4   r}   r   r   r   r)   r)   r)   r*   r      s(   *



=
/

$


;0r   alwaysc             K   s   dd l j}d }tj  tj| zy|d }W n tk
rL   |j }Y nX |j  |jd|j	d | f |}t
j| | tjjkrd|kstn|j	d|d< | f |}t
j| t
jdd}|j| W d Q R X W d t
j| X |S Q R X d S )Nr   rq   r;         T)Zreturn_filelike)r.   r/   warningscatch_warningssimplefilterKeyErrorZgcfZclfgetZadd_subplotr    Z"assert_is_valid_plot_return_objectpdZplottingZbootstrap_plotr6   Zensure_cleanZsavefigr,   )ffilterwarningskwargsr0   retZfigpathr)   r)   r*   _check_plot_works	  s,    






r   c              C   s   t jjt jjt\} }| S )N)osr   splitabspath__file__)pthr@   r)   r)   r*   curpath,  s    r   )r   )r   r   numpyr!   r   Zpandas.util._decoratorsr   Zpandas.util._test_decoratorsutilZ_test_decoratorstdZpandas.core.dtypes.apir   Zpandasr   r   r   Zpandas._testingZ_testingr    Zskip_if_no_mplr   r   r   r)   r)   r)   r*   <module>   s   
   w
#