o
    Û­jçA  ã                   @  s:  d Z ddlmZ ddlmZ ddlZddlmZ ddl	Z
ddl	mZ ddlmZ er6ddlmZ ddlmZ d>d
d„Zd>dd„Zdd„ Zd?dd„Zd?d@dd„Zdd„ ZdAdd„Zdd„ ZdBd d!„ZdCd#d$„ZdAd%d&„ZdDd(d)„ZdEd*d+„Z		d>d,d-„Z i fd.d/„Z!dFd1d2„Z"d3d4„ Z#d5d6„ Z$dGd8d9„Z%d:d;„ Z&d<d=„ Z'dS )HzF
Module consolidating common testing functions for checking plotting.
é    )Úannotations)ÚTYPE_CHECKINGN)Úis_list_like)ÚSeries)ÚSequence©ÚAxesTc                 C  sb   |r
|du r
t dƒ‚t| ƒ} | D ]}|r&| ¡ dusJ ‚t| ¡  ¡ |ƒ q| ¡ du s.J ‚qdS )a)  
    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_labelsÚ	get_texts)ÚaxesÚlabelsÚvisibleÚax© r   úY/var/www/html/CropPilot/venv/lib/python3.10/site-packages/pandas/tests/plotting/common.pyÚ_check_legend_labels   s   ûr   c                 C  sV   |r
|du r
t dƒ‚|r!|  ¡ \}}dd„ |D ƒ}||ksJ ‚dS |  ¡ du s)J ‚dS )a  
    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  ó   g | ]}|  ¡ ‘qS r   )Ú
get_marker)Ú.0Úhandler   r   r   Ú
<listcomp>@   ó    z(_check_legend_marker.<locals>.<listcomp>)r	   Úget_legend_handles_labelsr   )r   Úexpected_markersr   ÚhandlesÚ_Úmarkersr   r   r   Ú_check_legend_marker/   s   r    c           	      C  sn   ddl m} |  ¡ }| ¡ }t|ƒt|ƒksJ ‚t||ƒD ]\}}| ¡ }| ¡ }t ||¡ q| d¡ dS )zŒ
    Check each axes has identical lines

    Parameters
    ----------
    xp : matplotlib Axes object
    rs : matplotlib Axes object
    r   NÚall)	Úmatplotlib.pyplotÚpyplotÚ	get_linesÚlenÚzipÚ
get_xydataÚtmÚassert_almost_equalÚclose)	ÚxpÚrsÚpltÚxp_linesÚrs_linesÚxplÚrslÚxpdataÚrsdatar   r   r   Ú_check_dataF   s   	r4   c                 C  sB   ddl m} t| |ƒst| ƒs| g} | D ]
}| ¡ |ksJ ‚qdS )zà
    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.collectionsr5   Ú
isinstancer   Úget_visible)Úcollectionsr   r5   Úpatchr   r   r   Ú_check_visible]   s   ÿr;   r   úAxes | Sequence[Axes]ÚfilledÚboolÚreturnÚNonec                 C  s0   t | ƒ} | 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
   ÚpatchesÚfill)r   r=   r   r:   r   r   r   Ú_check_patches_all_filledq   s   
ÿÿrC   c                   s*   |   ¡ }tt||ƒƒ‰ ‡ fdd„| jD ƒS )Nc                   s   g | ]}ˆ | ‘qS r   r   ©r   Úv©Úmappedr   r   r   ‡   r   z&_get_colors_mapped.<locals>.<listcomp>)ÚuniqueÚdictr&   Úvalues)ÚseriesÚcolorsrH   r   rF   r   Ú_get_colors_mapped‚   s   rM   c                 C  s„  ddl m} ddlm}m}m} ddlm} |j}	|durp|dur.t	||ƒ}|dt
| ƒ… }t
| ƒt
|ƒks8J ‚t| |ƒD ]2\}
}t|
|ƒrP|
 ¡ }|	 |¡}nt|
||fƒr`t|
 ¡ d ƒ}n|
 ¡ }|	 |¡}||ksoJ ‚q=|dur¾|dur…t	||ƒ}|dt
| ƒ… }t
| ƒt
|ƒks�J ‚t| |ƒD ]+\}
}t|
|ƒr¤|
 ¡ d }n|
 ¡ }t|tjƒr²t|ƒ}|	 |¡}||ks½J ‚q”dS dS )aÿ  
    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   )r5   ÚLineCollectionÚPolyCollection)ÚLine2DN)Ú
matplotlibrL   r6   r5   rN   rO   Úmatplotlib.linesrP   ÚColorConverterrM   r%   r&   r7   Ú	get_colorÚto_rgbaÚtupleÚget_edgecolorÚget_facecolorÚnpÚndarray)r9   Ú
linecolorsÚ
facecolorsÚmappingrL   r5   rN   rO   rP   Úconvr:   ÚcolorÚresultÚexpectedr   r   r   Ú_check_colorsŠ   sB   





ïrb   c                 C  sb   t | ƒs|  ¡ |ksJ ‚dS dd„ | D ƒ}t|ƒt|ƒksJ ‚t||ƒD ]
\}}||ks.J ‚q$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  r   r   )Úget_text)r   Útr   r   r   r   Ø   r   z&_check_text_labels.<locals>.<listcomp>N)r   rc   r%   r&   )Útextsra   r   ÚlabelÚer   r   r   r   Ê   s   ÿr   c           	      C  s  ddl m} t| ƒ} | D ]z}|dus|durJt|j ¡ |ƒr#| ¡ }n
| ¡ |jdd� }|D ]}|dur=t | 	¡ |¡ |durIt | 
¡ |¡ q/|dusR|dur†t|j ¡ |ƒr_| ¡ }n
| ¡ |jdd� }|D ]}|duryt | 	¡ |¡ |dur…t | 
¡ |¡ qkqdS )ac  
    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)Úmatplotlib.tickerrh   r
   r7   ÚxaxisÚget_minor_formatterÚget_xticklabelsr(   r)   Úget_fontsizeÚget_rotationÚyaxisÚget_yticklabels)	r   Ú
xlabelsizeÚxrotÚ
ylabelsizeÚyrotrh   r   r   rf   r   r   r   Ú_check_ticks_propsÞ   s0   
€
€çrv   Úlinearc                 C  s:   t | ƒ} | D ]}|j ¡ |ksJ ‚|j ¡ |ksJ ‚qdS )zö
    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
   rk   Ú	get_scalerp   )r   rk   rp   r   r   r   r   Ú_check_ax_scales  s
   þry   c                 C  sê   ddl m} |du rd}t| ƒ}|dur+t|ƒ|ksJ ‚|D ]}t| ¡ ƒdks*J ‚q|durbtƒ }tƒ }|| ƒD ]}| ¡  ¡ }	| |	d d ¡ | |	d d ¡ q9t|ƒt|ƒf}
|
|ksbJ ‚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)gš™™™™™@g333333@é   )Údtype)Ú!pandas.plotting._matplotlib.toolsr{   r
   r%   Úget_childrenÚsetÚget_positionÚ
get_pointsÚaddr(   Úassert_numpy_array_equalÚfigureÚget_size_inchesrY   ÚarrayÚfloat64)r   Úaxes_numÚlayoutÚfigsizer{   Úvisible_axesr   Úx_setÚy_setÚpointsr`   r   r   r   Ú_check_axes_shape  s*   þr�   úSequence[Axes]c                 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   rz   c                 S  s   g | ]}|  ¡ r|‘qS r   )r8   )r   r   r   r   r   r   W  s    z$_flatten_visible.<locals>.<listcomp>)r~   r{   )r   r{   Úaxes_ndarrayr   r   r   r
   K  s   	r
   c           
      C  sv   t | ƒ} | D ]2}|j}d}d}|D ]}t|ddƒ}t|ddƒ}	|r%|d7 }|	r+|d7 }q||ks2J ‚||ks8J ‚qdS )zø
    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   ÚxerrÚyerrr   r•   Ú
xerr_countÚ
yerr_countÚcr“   r”   r   r   r   Ú_check_has_errorbars[  s    €ôrœ   c                 C  st  ddl m} t|tdœ}|du r7|du rd}t| || ƒsJ ‚|dkr3t| j|ƒs+J ‚t| jtƒs5J ‚dS dS |du rKt| ƒD ]	}t||ƒsHJ ‚q?dS t| tƒsRJ ‚t	|  
¡ ƒt	|ƒks^J ‚|  ¡ D ]U\}}t||| ƒsoJ ‚|dkr~|r}| ¡ |ks}J ‚qb|dkrž|r�|j ¡ |ks�J ‚t|j|ƒs•J ‚t|jtƒs�J ‚qb|dkr¶|d d }	|	j}
|rµ|
 ¡ |ksµJ ‚qbt‚dS )	a<  
    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   )rI   r   ÚbothNrI   r�   r   Úmedians)Úmatplotlib.axesr   rI   rV   r7   r   Úlinesr
   r   ÚsortedÚkeysÚitemsÚ	get_titler   ÚAssertionError)ÚreturnedÚreturn_typeÚexpected_keysÚcheck_ax_titler   ÚtypesÚrÚkeyÚvalueÚliner   r   r   r   Ú_check_box_return_typew  sH   þ€€ïr¯   c                   sŠ  dd l ‰ ‡ fdd„}d}|D ]´}ˆ j ddt|ƒ |¡ |d7 }ˆ jddd� | jdd	|i|¤Ž |ƒ r6J ‚ˆ j ¡  ˆ j ddt|ƒ |¡ |d7 }ˆ jdd
d� | jd|ddœ|¤Ž |ƒ rbJ ‚ˆ j ¡  |dvrÂˆ j ddt|ƒ |¡ |d7 }ˆ jdd
d� | jdd	|i|¤Ž |ƒ s‘J ‚ˆ j ¡  ˆ j ddt|ƒ |¡ |d7 }ˆ jddd� | jd|d
dœ|¤Ž |ƒ s½J ‚ˆ j ¡  qd S )Nr   c                    sN   ˆ j  ¡ j ¡ } ˆ j  ¡ j ¡ }tdd„ | D ƒƒ}tdd„ |D ƒƒ}|o%| S )Nc                 s  ó   � | ]	}|j  ¡  V  qd S ©N©Úgridliner8   ©r   Úgr   r   r   Ú	<genexpr>¼  ó   € z;_check_grid_settings.<locals>.is_grid_on.<locals>.<genexpr>c                 s  r°   r±   r²   r´   r   r   r   r¶   ½  r·   )r#   Úgcark   Úget_major_ticksrp   r!   )ÚxticksÚyticksÚxoffÚyoff©Úmplr   r   Ú
is_grid_on¹  s
   
z(_check_grid_settings.<locals>.is_grid_onr|   é   r   F)ÚgridÚkindT)rÃ   rÂ   )ÚpieÚhexbinÚscatterr   )rQ   r#   Úsubplotr%   ÚrcÚplotÚclf)ÚobjÚkindsÚkwsrÀ   ÚspndxrÃ   r   r¾   r   Ú_check_grid_settings´  s>   







€ärÏ   r_   c                   s   ‡ fdd„| d D ƒS )zL
    Auxiliary function for correctly unpacking cycler after MPL >= 1.5
    c                   s   g | ]}|ˆ  ‘qS r   r   rD   ©Úfieldr   r   r   å  r   z"_unpack_cycler.<locals>.<listcomp>zaxes.prop_cycler   )ÚrcParamsrÑ   r   rÐ   r   Ú_unpack_cyclerá  s   rÓ   c                 C  ó
   | j d S )NÚx©Ú_shared_axes©r   r   r   r   Ú
get_x_axisè  ó   
rÙ   c                 C  rÔ   )NÚyrÖ   rØ   r   r   r   Ú
get_y_axisì  rÚ   rÜ   Fc              	   K  sv   ddl m} |rt}nt}d}z%| d| ¡ ¡}| ¡  || |fi |¤ŽD ]}t |¡ q%W | 	|¡ |S | 	|¡ w )a×  
    Create plot and ensure that plot return object is valid.

    Parameters
    ----------
    f : func
        Plotting function.
    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…   )
r"   r#   Ú_gen_default_plotÚ_gen_two_subplotsÚgetÚgcfrÊ   r(   Ú"assert_is_valid_plot_return_objectr*   )ÚfÚdefault_axesÚkwargsr-   Ú	gen_plotsÚretÚfigr   r   r   Ú_check_plot_worksð  s   ÿ
þrè   c                 k  s   � | di |¤ŽV  dS )z'
    Create plot in a default way.
    Nr   r   ©râ   rç   rä   r   r   r   rÝ      s   €rÝ   c                 k  s`   � d|vr
|  d¡ | di |¤ŽV  | tjju rd|vsJ ‚n|  d¡|d< | di |¤ŽV  dS )z9
    Create plot on two subplots forcefully created.
    r   éÓ   éÔ   Nr   )Úadd_subplotÚpdÚplottingÚbootstrap_plotré   r   r   r   rÞ   '  s   €
rÞ   )NT)T)r   r<   r=   r>   r?   r@   )NNN)NNNN)rw   rw   )r   r<   r?   r‘   )r   r   )r_   )F)(Ú__doc__Ú
__future__r   Útypingr   ÚnumpyrY   Úpandas.core.dtypes.apir   Úpandasrí   r   Úpandas._testingÚ_testingr(   Úcollections.abcr   rŸ   r   r   r    r4   r;   rC   rM   rb   r   rv   ry   r�   r
   rœ   r¯   rÏ   rÓ   rÙ   rÜ   rè   rÝ   rÞ   r   r   r   r   Ú<module>   sB    



@

/

,

ÿ=
-
0