B
    [Ž0d3U  ã               @  sÔ   d Z ddlmZ ddlZddlmZmZ ddlZddlZ	ddl
mZ ddlm  mZ ddlmZ ddlZddlmZmZmZ ddlmZ eršddlmZ ejG d	d
„ d
ƒƒZddd„Zdd„ Zdd„ Z dd„ Z!dS )z²
Module consolidating common testing functions for checking plotting.

Currently all plotting tests are marked as slow via
``pytestmark = pytest.mark.slow`` at the module level.
é    )ÚannotationsN)ÚTYPE_CHECKINGÚSequence)Úcache_readonly)Úis_list_like)Ú	DataFrameÚSeriesÚto_datetime)ÚAxesc               @  sô   e Zd ZdZdd„ Zdd„ Zedd„ ƒZedd	„ ƒZd9dd„Z	d:dd„Z
dd„ Zd;dd„Zd<ddddœdd„Zdd„ Zd=dd„Zdd„ Zd>d d!„Zd?d#d$„Zd@d%d&„Zd'd(„ Zd)d*„ ZdAd,d-„ZdBd.d/„Zi fd0d1„ZdCd3d4„Zd5d6„ Zd7d8„ Zd
S )DÚTestPlotBasezE
    This is a common base class used for various plotting tests
    c             C  sT  dd l }ddlm} || _| ¡  d| _d| _| ¡ | _| ¡ | _| ¡ | _| 	¡ | _	d| _
d| _d| _d| _d	}t d
¡Š tjjddg|d}tjjdddg|d}t||tjjdd|dtjjdd|dtjjd|dttjj| j| j|tjdƒdœƒ| _W d Q R X t ¡ | _ttjjddtjjddt d¡tjjdd dœƒ| _d S )Nr   )Úcompatl     jÕ+2Zl     ÊRŠeöZé   é   )gš™™™™™@g333333@Úleftéd   é*   ZMaleZFemale)ÚsizeÚAÚBÚCéB   é   é¡   é    )r   Údtype)ÚgenderÚ	classroomÚheightÚweightÚcategoryÚdatetimeé   )r   r   r   )Ú
matplotlibZpandas.plotting._matplotlibr   Z
rcdefaultsZstart_date_to_int64Zend_date_to_int64Zmpl_ge_2_2_3Zmpl_ge_3_0_0Úmpl_ge_3_1_0Zmpl_ge_3_2_0Zbp_n_objectsZpolycollection_factorÚdefault_figsizeZdefault_tick_positionÚtmZ
RNGContextÚnpÚrandomÚchoicer   ÚnormalÚrandintr	   Zint64Zhist_dfZmakeTimeDataFrameZtdfÚuniformZarangeZ	hexbin_df)ÚselfÚmethodÚmplr   Únr   r   © r0   úN/var/www/html/venv/lib/python3.7/site-packages/pandas/tests/plotting/common.pyÚsetup_method*   sF    




zTestPlotBase.setup_methodc             C  s   t  ¡  d S )N)r%   Úclose)r,   r-   r0   r0   r1   Úteardown_methoda   s    zTestPlotBase.teardown_methodc             C  s   dd l m} |S )Nr   )Úmatplotlib.pyplotÚpyplot)r,   Úpltr0   r0   r1   r7   d   s    zTestPlotBase.pltc             C  s   dd l m} |jS )Nr   )Zmatplotlib.colorsÚcolorsZcolorConverter)r,   r8   r0   r0   r1   Úcolorconverterj   s    zTestPlotBase.colorconverterNTc             C  sj   |r|dkrt dƒ‚|  |¡}xF|D ]>}|rR| ¡ dk	s<t‚|  | ¡  ¡ |¡ q$| ¡ 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Úaxr0   r0   r1   Ú_check_legend_labelsp   s    

z!TestPlotBase._check_legend_labelsc             C  sT   |r|dkrt dƒ‚|r@| ¡ \}}dd„ |D ƒ}||ksPt‚n| ¡ 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 | ]}|  ¡ ‘qS r0   )Z
get_marker)Ú.0Úhandler0   r0   r1   ú
<listcomp>˜   s    z5TestPlotBase._check_legend_marker.<locals>.<listcomp>)r:   Zget_legend_handles_labelsr=   r<   )r,   rB   Zexpected_markersrA   ZhandlesÚ_Úmarkersr0   r0   r1   Ú_check_legend_marker‡   s    z!TestPlotBase._check_legend_markerc               sP   |  ¡ }|  ¡ }dd„ ‰ t|ƒt|ƒks,t‚‡ fdd„t||ƒD ƒ t ¡  dS )z¤
        Check each axes has identical lines

        Parameters
        ----------
        xp : matplotlib Axes object
        rs : matplotlib Axes object
        c             S  s    |   ¡ }|  ¡ }t ||¡ d S )N)Z
get_xydatar%   Úassert_almost_equal)ÚxplÚrslZxpdataZrsdatar0   r0   r1   Ú
check_line©   s    z,TestPlotBase._check_data.<locals>.check_linec               s   g | ]\}}ˆ ||ƒ‘qS r0   r0   )rD   rK   rL   )rM   r0   r1   rF   ¯   s    z,TestPlotBase._check_data.<locals>.<listcomp>N)Ú	get_linesÚlenr=   Úzipr%   r3   )r,   ZxpÚrsZxp_linesZrs_linesr0   )rM   r1   Ú_check_data   s    	zTestPlotBase._check_datac             C  sF   ddl m} t||ƒs$t|ƒs$|g}x|D ]}| ¡ |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.collectionsrS   Ú
isinstancer   Úget_visibler=   )r,   ÚcollectionsrA   rS   Úpatchr0   r0   r1   Ú_check_visible²   s
    
zTestPlotBase._check_visiblezAxes | Sequence[Axes]ÚboolÚNone)r?   ÚfilledÚreturnc             C  s:   |   |¡}x*|D ]"}x|jD ]}|j|kst‚qW qW dS )zÕ
        Check for each artist whether it is filled or not

        Parameters
        ----------
        axes : matplotlib Axes object, or its list-like
        filled : bool
            expected filling
        N)r;   ZpatchesÚfillr=   )r,   r?   r\   rB   rX   r0   r0   r1   Ú_check_patches_all_filledÅ   s    

z&TestPlotBase._check_patches_all_filledc               s*   |  ¡ }tt||ƒƒ‰ ‡ fdd„|jD ƒS )Nc               s   g | ]}ˆ | ‘qS r0   r0   )rD   Úv)Úmappedr0   r1   rF   Ü   s    z3TestPlotBase._get_colors_mapped.<locals>.<listcomp>)ÚuniqueÚdictrP   Úvalues)r,   Zseriesr8   rb   r0   )ra   r1   Ú_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|  ||¡}|dt|ƒ… }t|ƒt|ƒksft	‚xrt
||ƒD ]d\}
}t|
|ƒr˜|
 ¡ }|	 |¡}n(t|
||fƒr¸t|
 ¡ d ƒ}n|
 ¡ }|	 |¡}||ksrt	‚qrW |dk	rŠ|dk	r
|  ||¡}|dt|ƒ… }t|ƒt|ƒks t	‚xht
||ƒD ]Z\}
}t|
|ƒrN|
 ¡ d }n|
 ¡ }t|tjƒrlt|ƒ}|	 |¡}||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   )rS   ÚLineCollectionÚPolyCollection)ÚLine2DN)rT   rS   rf   rg   Zmatplotlib.linesrh   r9   re   rO   r=   rP   rU   Ú	get_colorZto_rgbaÚtupleZget_edgecolorZget_facecolorr&   Zndarray)r,   rW   Z
linecolorsZ
facecolorsÚmappingrS   rf   rg   rh   ÚconvrX   ÚcolorÚresultÚexpectedr0   r0   r1   Ú_check_colorsÞ   s<    




zTestPlotBase._check_colorsc             C  sd   t |ƒs| ¡ |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 | ]}|  ¡ ‘qS r0   )Úget_text)rD   Útr0   r0   r1   rF   .  s    z3TestPlotBase._check_text_labels.<locals>.<listcomp>N)r   rq   r=   rO   rP   )r,   Ztextsro   r@   ÚlabelÚer0   r0   r1   r>      s    zTestPlotBase._check_text_labelsc       
      C  s&  ddl m} |  |¡}x|D  ]þ}|dk	s4|dk	r t|j ¡ |ƒrN| ¡ }n| ¡ |jdd }x<|D ]4}	|dk	r„t |	 	¡ |¡ |dk	rht |	 
¡ |¡ qhW |dk	s°|dk	rt|j ¡ |ƒrÊ| ¡ }n| ¡ |jdd }x>|D ]6}	|dk	rt |	 	¡ |¡ |dk	rät |	 
¡ |¡ qäW 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.tickerru   r;   rU   ÚxaxisZget_minor_formatterZget_xticklabelsr%   rJ   Zget_fontsizeZget_rotationÚyaxisZget_yticklabels)
r,   r?   Z
xlabelsizeZxrotZ
ylabelsizeZyrotru   rB   r@   rs   r0   r0   r1   Ú_check_ticks_props3  s*    





zTestPlotBase._check_ticks_propsÚlinearc             C  s@   |   |¡}x0|D ](}|j ¡ |ks&t‚|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)r;   rw   Z	get_scaler=   rx   )r,   r?   rw   rx   rB   r0   r0   r1   Ú_check_ax_scalesc  s    

zTestPlotBase._check_ax_scalesc       	      C  s¦   ddl m} |dkr| j}|  |¡}|dk	r^t|ƒ|ks<t‚x |D ]}t| ¡ ƒdksBt‚qBW |dk	r€|  ||ƒ¡}||ks€t‚t 	|d 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   )Úflatten_axesN)r   )Ú!pandas.plotting._matplotlib.toolsr|   r$   r;   rO   r=   Zget_childrenÚ_get_axes_layoutr%   Zassert_numpy_array_equalÚfigureZget_size_inchesr&   ÚarrayZfloat64)	r,   r?   Zaxes_numZlayoutZfigsizer|   Zvisible_axesrB   rn   r0   r0   r1   Ú_check_axes_shapet  s    

zTestPlotBase._check_axes_shapec             C  sZ   t ƒ }t ƒ }x<|D ]4}| ¡  ¡ }| |d d ¡ | |d d ¡ qW t|ƒt|ƒfS )Nr   é   )ÚsetZget_positionZ
get_pointsÚaddrO   )r,   r?   Zx_setZy_setrB   Zpointsr0   r0   r1   r~   ˜  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   )r|   c             S  s   g | ]}|  ¡ r|‘qS r0   )rV   )rD   rB   r0   r0   r1   rF   ®  s    z1TestPlotBase._flatten_visible.<locals>.<listcomp>)r}   r|   )r,   r?   r|   r0   r0   r1   r;   ¢  s    	zTestPlotBase._flatten_visibler   c             C  s€   |   |¡}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_yerrr‚   N)r;   Ú
containersÚgetattrr=   )r,   r?   ZxerrZyerrrB   r‡   Z
xerr_countZ
yerr_countÚcr…   r†   r0   r0   r1   Ú_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r˜x |  |¡D ]}t||ƒs~t‚q~W dS t|t	ƒs¦t‚t
| ¡ ƒt
|ƒks¾t‚xÎ| ¡ D ]Â\}}	t|	|| ƒsât‚|dkr|rŠ|	 ¡ |ksŠt‚qÈ|dkrP|r*|	j ¡ |ks*t‚t|	j|ƒs<t‚t|	jtƒsŠt‚qÈ|dkr†|	d d }
|
j}|rŠ| ¡ |ksŠt‚qÈt‚qÈW 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   )r
   )rc   r?   ÚbothNrc   r‹   r?   Zmedians)Úmatplotlib.axesr
   rc   rj   rU   r=   rB   Úlinesr;   r   ÚsortedÚkeysÚitemsZ	get_titler?   )r,   ÚreturnedÚreturn_typeZexpected_keysZcheck_ax_titler
   ÚtypesÚrÚkeyÚvalueÚliner?   r0   r0   r1   Ú_check_box_return_typeÌ  s>    


z#TestPlotBase._check_box_return_typec               sl  dd l }‡ fdd„}d}xL|D ]B}ˆ j ddt|ƒ |¡ |d7 }|jddd |jf d	|i|—Ž |ƒ rrt‚ˆ j ddt|ƒ |¡ |d7 }|jdd
d |jf |ddœ|—Ž |ƒ rÀt‚|dkr ˆ j ddt|ƒ |¡ |d7 }|jdd
d |jf d	|i|—Ž |ƒ st‚ˆ 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rLtdd„ | D ƒƒ}tdd„ |D ƒƒ}n$tdd„ | D ƒƒ}tdd„ |D ƒƒ}|ov| S )Nc             s  s   | ]}|j  ¡  V  qd S )N)ÚgridlinerV   )rD   Úgr0   r0   r1   ú	<genexpr>  s    zHTestPlotBase._check_grid_settings.<locals>.is_grid_on.<locals>.<genexpr>c             s  s   | ]}|j  ¡  V  qd S )N)r™   rV   )rD   rš   r0   r0   r1   r›     s    c             s  s   | ]}|j  V  qd S )N)ÚgridOn)rD   rš   r0   r0   r1   r›     s    c             s  s   | ]}|j  V  qd S )N)rœ   )rD   rš   r0   r0   r1   r›     s    )r7   Zgcarw   Zget_major_ticksrx   r#   Úall)ZxticksZyticksZxoffZyoff)r,   r0   r1   Ú
is_grid_on  s    z5TestPlotBase._check_grid_settings.<locals>.is_grid_onr‚   r   r?   F)ÚgridÚkindT)r    rŸ   )ÚpieZhexbinZscatter)r"   r7   ZsubplotrO   ÚrcZplotr=   )r,   ÚobjÚkindsÚkwsr.   rž   Zspndxr    r0   )r,   r1   Ú_check_grid_settings  s2    

z!TestPlotBase._check_grid_settingsrm   c               s   ‡ fdd„|d D ƒS )zT
        Auxiliary function for correctly unpacking cycler after MPL >= 1.5
        c               s   g | ]}|ˆ  ‘qS r0   r0   )rD   r`   )Úfieldr0   r1   rF   <  s    z/TestPlotBase._unpack_cycler.<locals>.<listcomp>zaxes.prop_cycler0   )r,   ZrcParamsr§   r0   )r§   r1   Ú_unpack_cycler8  s    zTestPlotBase._unpack_cyclerc             C  s   | j  ¡ r|jd S |jS )NÚx)r   Úmpl_ge_3_5_0Ú_shared_axesZ_shared_x_axes)r,   rB   r0   r0   r1   Ú
get_x_axis>  s    zTestPlotBase.get_x_axisc             C  s   | j  ¡ r|jd S |jS )NÚy)r   rª   r«   Z_shared_y_axes)r,   rB   r0   r0   r1   Ú
get_y_axisA  s    zTestPlotBase.get_y_axis)NT)NT)T)T)NNN)NNNN)rz   rz   )NNN)r   r   )NT)rm   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r2   r4   r   r7   r9   rC   rI   rR   rY   r_   re   rp   r>   ry   r{   r   r~   r;   rŠ   r˜   r¦   r¨   r¬   r®   r0   r0   r0   r1   r   $   s0   7



A
/

$


;0
r   ÚalwaysFc       
      K  sÚ   ddl m} |rt}nt}d}t ¡ ª t |¡ zŒy`| d| ¡ ¡}| 	¡  x || |f|ŽD ]}t
 |¡ q^W t
jdd}| |¡ W dQ R X W n& tk
rº }	 z|	‚W dd}	~	X Y nX W dt
 |¡ X |S Q R X dS )aT  
    Create plot and ensure that plot return object is valid.

    Parameters
    ----------
    f : func
        Plotting function.
    filterwarnings : str
        Warnings filter.
        See https://docs.python.org/3/library/warnings.html#warning-filter
    default_axes : bool, optional
        If False (default):
            - If `ax` not in `kwargs`, then create subplot(211) and plot there
            - Create new subplot(212) and plot there as well
            - Mind special corner case for bootstrap_plot (see `_gen_two_subplots`)
        If True:
            - Simply run plotting function with kwargs provided
            - All required axes instances will be created automatically
            - It is recommended to use it when the plotting function
            creates multiple axes itself. It helps avoid warnings like
            'UserWarning: To output multiple subplots,
            the figure containing the passed axes is being cleared'
    **kwargs
        Keyword arguments passed to the plotting function.

    Returns
    -------
    Plot object returned by the last plotting.
    r   Nr   T)Zreturn_filelike)r5   r6   Ú_gen_default_plotÚ_gen_two_subplotsÚwarningsÚcatch_warningsÚsimplefilterÚgetZgcfZclfr%   Z"assert_is_valid_plot_return_objectZensure_cleanZsavefigÚ	Exceptionr3   )
ÚfÚfilterwarningsZdefault_axesÚkwargsr7   Z	gen_plotsÚretÚfigÚpathÚerrr0   r0   r1   Ú_check_plot_worksE  s$    

rÂ   c             k  s   | f |ŽV  dS )z'
    Create plot in a default way.
    Nr0   )r»   r¿   r½   r0   r0   r1   r´     s    r´   c             k  sV   d|kr|  d¡ | f |ŽV  | tjjkr8d|ksFt‚n|  d¡|d< | f |ŽV  dS )z9
    Create plot on two subplots forcefully created.
    rB   éÓ   éÔ   N)Zadd_subplotÚpdZplottingZbootstrap_plotr=   )r»   r¿   r½   r0   r0   r1   rµ   †  s    
rµ   c              C  s   t j t j t¡¡\} }| S )N)ÚosrÀ   ÚsplitÚabspathÚ__file__)ÚpthrG   r0   r0   r1   Úcurpath•  s    rË   )r³   F)"r²   Ú
__future__r   rÆ   Útypingr   r   r¶   Únumpyr&   Zpandas.util._decoratorsr   Zpandas.util._test_decoratorsÚutilZ_test_decoratorsÚtdZpandas.core.dtypes.apir   ZpandasrÅ   r   r   r	   Zpandas._testingZ_testingr%   rŒ   r
   Zskip_if_no_mplr   rÂ   r´   rµ   rË   r0   r0   r0   r1   Ú<module>   s,       %
: