a
    nÝEb3U  ã                   @  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  sh  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g d¢|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   ƒ n1 �s0    Y  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é   )Ú
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   © r1   ú\/home/ja/django-apps/lartica_env/lib/python3.9/site-packages/pandas/tests/plotting/common.pyÚsetup_method*   sR    



üÿúÿ&
ýÿzTestPlotBase.setup_methodc                 C  s   t  ¡  d S ©N)r&   Úclose)r-   r.   r1   r1   r2   Úteardown_methoda   s    zTestPlotBase.teardown_methodc                 C  s   dd l m} |S ©Nr   )Úmatplotlib.pyplotÚpyplot)r-   Úpltr1   r1   r2   r:   d   s    zTestPlotBase.pltc                 C  s   dd l m} |jS r7   )Zmatplotlib.colorsÚcolorsZcolorConverter)r-   r;   r1   r1   r2   Úcolorconverterj   s    zTestPlotBase.colorconverterNTc                 C  sf   |r|du rt dƒ‚|  |¡}|D ]>}|rP| ¡ dus:J ‚|  | ¡  ¡ |¡ q"| ¡ du s"J ‚q"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Ú_check_text_labelsZ	get_texts)r-   ÚaxesÚlabelsÚvisibleÚaxr1   r1   r2   Ú_check_legend_labelsp   s    
z!TestPlotBase._check_legend_labelsc                 C  sT   |r|du rt dƒ‚|r@| ¡ \}}dd„ |D ƒ}||ksPJ ‚n| ¡ du sPJ ‚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 r1   )Z
get_marker)Ú.0Úhandler1   r1   r2   Ú
<listcomp>˜   ó    z5TestPlotBase._check_legend_marker.<locals>.<listcomp>)r=   Zget_legend_handles_labelsr?   )r-   rD   Zexpected_markersrC   ZhandlesÚ_Úmarkersr1   r1   r2   Ú_check_legend_marker‡   s    z!TestPlotBase._check_legend_markerc                   sP   |  ¡ }|  ¡ }dd„ ‰ t|ƒt|ƒks,J ‚‡ 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 r4   )Z
get_xydatar&   Úassert_almost_equal)ÚxplÚrslZxpdataZrsdatar1   r1   r2   Ú
check_line©   s    z,TestPlotBase._check_data.<locals>.check_linec                   s   g | ]\}}ˆ ||ƒ‘qS r1   r1   )rF   rN   rO   ©rP   r1   r2   rH   ¯   rI   z,TestPlotBase._check_data.<locals>.<listcomp>N)Ú	get_linesÚlenÚzipr&   r5   )r-   ZxpÚrsZxp_linesZrs_linesr1   rQ   r2   Ú_check_data�   s    	zTestPlotBase._check_datac                 C  sB   ddl m} t||ƒs$t|ƒs$|g}|D ]}| ¡ |ks(J ‚q(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.collectionsrW   Ú
isinstancer   Úget_visible)r-   ÚcollectionsrC   rW   Úpatchr1   r1   r2   Ú_check_visible²   s
    zTestPlotBase._check_visiblezAxes | Sequence[Axes]ÚboolÚNone)rA   ÚfilledÚreturnc                 C  s2   |   |¡}|D ]}|jD ]}|j|ksJ ‚qq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Úfill)r-   rA   r`   rD   r\   r1   r1   r2   Ú_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 r1   r1   ©rF   Úv©Zmappedr1   r2   rH   Ü   rI   z3TestPlotBase._get_colors_mapped.<locals>.<listcomp>)ÚuniqueÚdictrT   Úvalues)r-   Zseriesr;   rg   r1   rf   r2   Ú_get_colors_mapped×   s    zTestPlotBase._get_colors_mappedc                 C  s†  ddl m}m}m} ddlm} | j}	|durÖ|durR|  ||¡}|dt|ƒ… }t|ƒt|ƒksfJ ‚t	||ƒD ]d\}
}t
|
|ƒr–|
 ¡ }|	 |¡}n(t
|
||fƒr¶t|
 ¡ d ƒ}n|
 ¡ }|	 |¡}||kspJ ‚qp|du�r‚|du�r|  ||¡}|dt|ƒ… }t|ƒt|ƒk�sJ ‚t	||ƒD ]Z\}
}t
|
|ƒ�rH|
 ¡ d }n|
 ¡ }t
|tjƒ�rft|ƒ}|	 |¡}||k�s&J ‚�q&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   )rW   ÚLineCollectionÚPolyCollection)ÚLine2DN)rX   rW   rk   rl   Zmatplotlib.linesrm   r<   rj   rS   rT   rY   Ú	get_colorZto_rgbaÚtupleZget_edgecolorZget_facecolorr'   Zndarray)r-   r[   Z
linecolorsZ
facecolorsÚmappingrW   rk   rl   rm   Úconvr\   ÚcolorÚresultÚexpectedr1   r1   r2   Ú_check_colorsÞ   s<    




zTestPlotBase._check_colorsc                 C  s`   t |ƒs| ¡ |ks\J ‚nBdd„ |D ƒ}t|ƒt|ƒks<J ‚t||ƒD ]\}}||ksFJ ‚qFd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 r1   )Úget_text)rF   Útr1   r1   r2   rH   .  rI   z3TestPlotBase._check_text_labels.<locals>.<listcomp>N)r   rv   rS   rT   )r-   Ztextsrt   rB   ÚlabelÚer1   r1   r2   r@      s    zTestPlotBase._check_text_labelsc           
      C  s  ddl m} |  |¡}|D ]ô}|dus.|dur–t|j ¡ |ƒrH| ¡ }n| ¡ |jdd� }|D ]4}	|dur|t |	 	¡ |¡ |dur`t |	 
¡ |¡ q`|dus¦|durt|j ¡ |ƒrÀ| ¡ }n| ¡ |jdd� }|D ]4}	|durôt |	 	¡ |¡ |durØt |	 
¡ |¡ qØq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.tickerrz   r>   rY   ÚxaxisZget_minor_formatterZget_xticklabelsr&   rM   Zget_fontsizeZget_rotationÚyaxisZget_yticklabels)
r-   rA   Z
xlabelsizeZxrotZ
ylabelsizeZyrotrz   rD   rB   rx   r1   r1   r2   Ú_check_ticks_props3  s*    


zTestPlotBase._check_ticks_propsÚlinearc                 C  s<   |   |¡}|D ](}|j ¡ |ks$J ‚|j ¡ |ksJ ‚q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>   r|   Z	get_scaler}   )r-   rA   r|   r}   rD   r1   r1   r2   Ú_check_ax_scalesc  s    
zTestPlotBase._check_ax_scalesc           	      C  s¢   ddl m} |du r| j}|  |¡}|durZt|ƒ|ks<J ‚|D ]}t| ¡ ƒdks@J ‚q@|dur||  ||ƒ¡}||ks|J ‚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>   rS   Zget_childrenÚ_get_axes_layoutr&   Zassert_numpy_array_equalÚfigureZget_size_inchesr'   ÚarrayZfloat64)	r-   rA   Zaxes_numZlayoutZfigsizer‚   Zvisible_axesrD   rs   r1   r1   r2   Ú_check_axes_shapet  s    
þzTestPlotBase._check_axes_shapec                 C  sV   t ƒ }t ƒ }|D ]4}| ¡  ¡ }| |d d ¡ | |d d ¡ qt|ƒt|ƒfS )Nr   é   )ÚsetÚget_positionZ
get_pointsÚaddrS   )r-   rA   Zx_setZy_setrD   Zpointsr1   r1   r2   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 r1   )rZ   )rF   rD   r1   r1   r2   rH   ®  rI   z1TestPlotBase._flatten_visible.<locals>.<listcomp>)rƒ   r‚   )r-   rA   r‚   r1   r1   r2   r>   ¢  s    	zTestPlotBase._flatten_visibler   c                 C  sx   |   |¡}|D ]d}|j}d}d}|D ]4}t|ddƒ}	t|ddƒ}
|	rL|d7 }|
r$|d7 }q$||ksfJ ‚||ksJ ‚q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Úgetattr)r-   rA   ZxerrZyerrrD   rŽ   Z
xerr_countZ
yerr_countÚcrŒ   r�   r1   r1   r2   Ú_check_has_errorbars±  s    

z!TestPlotBase._check_has_errorbarsc                 C  s„  ddl m} t|tdœ}|du rj|du r,d}t||| ƒs>J ‚|dkrft|j|ƒsVJ ‚t|jtƒsfJ ‚�n|du r”|  |¡D ]}t||ƒs|J ‚q|dS t|tƒs¢J ‚t	| 
¡ ƒt	|ƒksºJ ‚| ¡ D ]¼\}}	t|	|| ƒsÜJ ‚|dkrú|rø|	 ¡ |ksøJ ‚qÂ|dk�rD|�r|	j ¡ |k�sJ ‚t|	j|ƒ�s0J ‚t|	jtƒ�s~J ‚qÂ|dk�rz|	d d }
|
j}|�r~| ¡ |k�s~J ‚qÂt‚qÂ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
   )rh   rA   ÚbothNrh   r’   rA   Zmedians)Úmatplotlib.axesr   rh   ro   rY   rD   Úlinesr>   r   ÚsortedÚkeysÚitemsZ	get_titlerA   ÚAssertionError)r-   ÚreturnedÚreturn_typeZexpected_keysZcheck_ax_titler   ÚtypesÚrÚkeyÚvalueÚlinerA   r1   r1   r2   Ú_check_box_return_typeÌ  s>    

z#TestPlotBase._check_box_return_typec                   sf  dd l }‡ fdd„}d}|D �]B}ˆ j ddt|ƒ |¡ |d7 }|jddd� |jf d	|i|¤Ž |ƒ rnJ ‚ˆ j ddt|ƒ |¡ |d7 }|jdd
d� |jf |ddœ|¤Ž |ƒ r¼J ‚|dvrˆ j ddt|ƒ |¡ |d7 }|jdd
d� |jf d	|i|¤Ž |ƒ �sJ ‚ˆ j ddt|ƒ |¡ |d7 }|jddd� |jf |d
dœ|¤Ž |ƒ sJ ‚q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 r4   ©ZgridlinerZ   ©rF   Úgr1   r1   r2   Ú	<genexpr>  rI   zHTestPlotBase._check_grid_settings.<locals>.is_grid_on.<locals>.<genexpr>c                 s  s   | ]}|j  ¡  V  qd S r4   r¡   r¢   r1   r1   r2   r¤     rI   c                 s  s   | ]}|j  V  qd S r4   ©ZgridOnr¢   r1   r1   r2   r¤     rI   c                 s  s   | ]}|j  V  qd S r4   r¥   r¢   r1   r1   r2   r¤     rI   )r:   Zgcar|   Zget_major_ticksr}   r$   Úall)ZxticksZyticksZxoffZyoff©r-   r1   r2   Ú
is_grid_on  s    z5TestPlotBase._check_grid_settings.<locals>.is_grid_onrˆ   r   rA   F)ÚgridÚkindT)rª   r©   )ÚpieZhexbinZscatter)r#   r:   ZsubplotrS   ÚrcZplot)r-   ÚobjÚkindsÚkwsr/   r¨   Zspndxrª   r1   r§   r2   Ú_check_grid_settings  s2    


z!TestPlotBase._check_grid_settingsrr   c                   s   ‡ fdd„|d D ƒS )zT
        Auxiliary function for correctly unpacking cycler after MPL >= 1.5
        c                   s   g | ]}|ˆ  ‘qS r1   r1   rd   ©Úfieldr1   r2   rH   <  rI   z/TestPlotBase._unpack_cycler.<locals>.<listcomp>zaxes.prop_cycler1   )r-   ZrcParamsr²   r1   r±   r2   Ú_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-   rD   r1   r1   r2   Ú
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_axesr·   r1   r1   r2   Ú
get_y_axisA  s    zTestPlotBase.get_y_axis)NT)NT)T)T)NNN)NNNN)r   r   )NNN)r   r   )NT)rr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r3   r6   r   r:   r<   rE   rL   rV   r]   rc   rj   ru   r@   r~   r€   r‡   r„   r>   r‘   r    r°   r³   r¸   rº   r1   r1   r1   r2   r   $   s<   7




 ÿ ÿ
B ÿ
0

$

 ÿ
<0
r   ÚalwaysFc           
      K  s  ddl m} |rt}nt}d}t ¡ �Ø t |¡ z¬zt| d| ¡ ¡}| 	¡  || |fi |¤ŽD ]}t
 |¡ q`t
jdd��}| |¡ W d  ƒ n1 sœ0    Y  W n( tyÐ }	 z|	‚W Y d}	~	n
d}	~	0 0 W t
 |¡ nt
 |¡ 0 |W  d  ƒ S 1 �s0    Y  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)r8   r9   Ú_gen_default_plotÚ_gen_two_subplotsÚwarningsÚcatch_warningsÚsimplefilterÚgetZgcfZclfr&   Z"assert_is_valid_plot_return_objectZensure_cleanZsavefigÚ	Exceptionr5   )
ÚfÚfilterwarningsZdefault_axesÚkwargsr:   Z	gen_plotsÚretÚfigÚpathÚerrr1   r1   r2   Ú_check_plot_worksE  s$    

,rÎ   c                 k  s   | f i |¤ŽV  dS )z'
    Create plot in a default way.
    Nr1   ©rÇ   rË   rÉ   r1   r1   r2   rÀ     s    rÀ   c                 k  s^   d|vr|  d¡ | f i |¤ŽV  | tjju r<d|vsJJ ‚n|  d¡|d< | f i |¤ŽV  dS )z9
    Create plot on two subplots forcefully created.
    rD   éÓ   éÔ   N)Zadd_subplotÚpdZplottingZbootstrap_plotrÏ   r1   r1   r2   rÁ   †  s    
rÁ   c                  C  s   t j t j t¡¡\} }| S r4   )ÚosrÌ   ÚsplitÚabspathÚ__file__)ÚpthrJ   r1   r1   r2   Ú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Ø   r1   r1   r1   r2   Ú<module>   s.       $
: