U
    ùQ”i¹D  ã                
   @  sP  d dl mZ d dlmZmZmZ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 d dlmZ d dlmZ d dlZd dlm  mZ d d	lmZ d d
lmZmZ d dlm Z  d dl!m"Z" d dl#m$Z$m%Z%m&Z& erîd dl'm(Z( d dl)m*Z* G dd„ deƒZ+d!dddœdd„Z,d"dddœdd„Z-d#dddœdd„Z.d$ddddddœdd „Z/dS )%é    )Úannotations)ÚTYPE_CHECKINGÚ
CollectionÚLiteralÚ
NamedTupleN)Úsetp)ÚMatplotlibColor)Úfind_stack_level)Úis_dict_like©Úremove_na_arraylike©Úpprint_thing)ÚLinePlotÚMPLPlot)Úcreate_iter_data_given_by)Úget_standard_colors)Úcreate_subplotsÚflatten_axesÚmaybe_adjust_figure)ÚAxes)ÚLine2Dc                      sò   e Zd Zeddœdd„ƒZdZdZG dd„ deƒZd-d
ddœdd„Z	ddœdd„Z
ed.d
dœdd„ƒZdd„ Zd/dddœdd„Zddœdd„Zddœdd„Zd dd!œd"d#„Zddœd$d%„Zddœd&d'„Zed(dœd)d*„ƒZe‡ fd+d,„ƒZ‡  ZS )0ÚBoxPlotzLiteral['box']©Úreturnc                 C  s   dS )NZbox© ©Úselfr   r   úG/tmp/pip-unpacked-wheel-58ppya8e/pandas/plotting/_matplotlib/boxplot.pyÚ_kind*   s    zBoxPlot._kindÚ
horizontal)NÚaxesÚdictÚbothc                   @  s   e Zd ZU ded< ded< dS )z
BoxPlot.BPr   Úaxzdict[str, list[Line2D]]ÚlinesN)Ú__name__Ú
__module__Ú__qualname__Ú__annotations__r   r   r   r   ÚBP2   s   
r*   r!   ÚstrÚNone)Úreturn_typer   c                 K  s,   || j krtdƒ‚|| _tj| |f|Ž d S )Nz2return_type must be {None, 'axes', 'dict', 'both'})Ú_valid_return_typesÚ
ValueErrorr-   r   Ú__init__)r   Údatar-   Úkwargsr   r   r   r0   7   s    
zBoxPlot.__init__c                 C  s"   | j r| jdkrd| _nd| _d S )NÚverticalF)ÚsubplotsÚorientationÚsharexÚshareyr   r   r   r   Ú_args_adjust?   s    
zBoxPlot._args_adjustN)r-   c                 K  st   |j dkr(dd„ |D ƒ}dd„ |D ƒ}nt|ƒ}|j|f|Ž}|dkrN||fS |dkrh| j||d�|fS ||fS d S )Né   c                 S  s   g | ]}t |ƒ‘qS r   r   ©Ú.0Úvr   r   r   Ú
<listcomp>N   s     z!BoxPlot._plot.<locals>.<listcomp>c                 S  s(   g | ] }|j d kr|nt tjg¡‘qS )r   )ÚsizeÚnpÚarrayÚnanr:   r   r   r   r=   R   s     r"   r#   ©r$   r%   )Úndimr   Úboxplotr*   )Úclsr$   ÚyÚ
column_numr-   ÚkwdsÚbpr   r   r   Ú_plotI   s    
zBoxPlot._plotc                 C  s¶   d| j krt| jd k	r$tjdtƒ d� | j  d¡| _t| jtƒrzddddg}| jD ] }||krPt	d|› d	|› �ƒ‚qPnd | _t
d
| jd d�}|d | _|d | _|d | _|d | _d S )NÚcolorzC'color' and 'colormap' cannot be used simultaneously. Using 'color')Ú
stacklevelÚboxesÚwhiskersÚmediansÚcapsú!color dict contains invalid key 'ú'. The key must be either é   )Ú
num_colorsÚcolormaprK   r   r9   )rH   rU   ÚwarningsÚwarnr	   ÚpoprK   Ú
isinstancer"   r/   r   Ú_boxes_cÚ_whiskers_cÚ
_medians_cÚ_caps_c)r   Ú
valid_keysÚkeyÚcolorsr   r   r   Ú_validate_color_args^   s(    

ý
ÿ


zBoxPlot._validate_color_argsrK   zQdict[str, MatplotlibColor] | MatplotlibColor | Collection[MatplotlibColor] | None)Ú
color_kwdsr   c                 C  s   d S ©Nr   )r   rT   rb   r   r   r   Ú_get_colors}   s    zBoxPlot._get_colorsc                 C  sú   t | jtƒrN| j d| j¡}| j d| j¡}| j d| j¡}| j d| j¡}n0| jpX| j}| jpd| j}| jpp| j}| jp|| j}| j d¡sœt	|d |dd� | j d¡sºt	|d |dd� | j d	¡sØt	|d |dd� | j d
¡söt	|d |dd� d S )NrM   rN   rO   rP   Úboxpropsé   ©rK   ÚalphaÚwhiskerpropsÚmedianpropsÚcapprops)
rY   rK   r"   ÚgetrZ   r[   r\   r]   rH   r   )r   rI   rM   rN   rO   rP   r   r   r   Úmaybe_color_bp‡   s"    zBoxPlot.maybe_color_bpc                 C  s„  | j rètjtd�| _| jd k	r,t| j| jƒn| j}t	| j
|d�ƒD ]¢\}\}}|  |¡}| j ¡ }| jd k	rš|j}| t|ƒ¡ dd„ | jjjd D ƒ}n
t|ƒg}| j||f|| jdœ|—Ž\}}	|  |	¡ || j|< |  ||¡ qBn˜| jjj}|  d¡}| j ¡ }| j||fd| jdœ|—Ž\}}	|  |	¡ || _dd„ |  
¡ D ƒ}
dd„ |
D ƒ}
| j�std	d„ tt|
ƒƒD ƒ}
|  ||
¡ d S )
N©Zdtype)r1   c                 S  s   g | ]}t |ƒ‘qS r   r   )r;   Úcolr   r   r   r=   ·   s    z&BoxPlot._make_plot.<locals>.<listcomp>r   )rG   r-   c                 S  s   g | ]\}}|‘qS r   r   )r;   ÚleftÚ_r   r   r   r=   Î   s     c                 S  s   g | ]}t |ƒ‘qS r   r   )r;   rp   r   r   r   r=   Ï   s     c                 S  s   g | ]}t |ƒ‘qS r   r   )r;   r_   r   r   r   r=   Ñ   s     )r4   ÚpdÚSeriesÚobjectÚ_return_objÚbyr   r1   r   Ú	enumerateZ
_iter_dataZ_get_axrH   ÚcopyÚTÚ	set_titler   ÚcolumnsÚlevelsrJ   r-   rm   Ú_set_ticklabelsÚvaluesZ	use_indexÚrangeÚlen)r   r1   ÚiÚlabelrF   r$   rH   Z
ticklabelsÚretrI   Úlabelsr   r   r   Ú
_make_plot    s^    ÿý


ÿ
 ÿ ÿÿ





 ÿ ÿÿ

zBoxPlot._make_plotr   )r$   r   c                 C  s$   | j dkr| |¡ n
| |¡ d S )Nr3   )r5   Úset_xticklabelsÚset_yticklabels)r   r$   r„   r   r   r   r}   Ô   s    
zBoxPlot._set_ticklabelsc                 C  s   d S rc   r   r   r   r   r   Ú_make_legendÚ   s    zBoxPlot._make_legendc                 C  s0   | j r| t| j ƒ¡ | jr,| t| jƒ¡ d S rc   )ÚxlabelÚ
set_xlabelr   ÚylabelÚ
set_ylabel)r   r$   r1   r   r   r   Ú_post_plot_logicÝ   s    zBoxPlot._post_plot_logicz#Literal[('horizontal', 'vertical')]c                 C  s   | j  dd¡rdS dS d S )NÚvertTr3   r    )rH   rl   r   r   r   r   r5   ä   s    zBoxPlot.orientationc                   s   | j d krtƒ jS | jS d S rc   )r-   ÚsuperÚresultru   r   ©Ú	__class__r   r   r�   ë   s    
zBoxPlot.result)r!   )Nr!   )NrK   )r&   r'   r(   Úpropertyr   Z_layout_typer.   r   r*   r0   r8   ÚclassmethodrJ   ra   rd   rm   r…   r}   rˆ   r�   r5   r�   Ú__classcell__r   r   r‘   r   r   )   s0   
   ÿ!  ú
4r   TFÚbool)Únumeric_onlyÚgridc
                 K  sp  |  |¡}|d kr6t|ttfƒs&|g}| ¡ j |¡}t|ƒ}t||
 	dd¡|
 	dd¡|||d�\}}t
|ƒ}|
 	dd ¡|
 	dd ¡ }}|
 dd¡rž|pš|}n|p¤|}g }t|ƒD ]\\}}|| }|| }t|Ž \}}| |||f||dœ|
—Ž}| |¡ | |¡ | |¡ q²tj||d	d
�}|	d k�r.|}t|ƒdk�rD|d n|}| d|› �¡ t|dddddd� |S )Nr6   Tr7   )Únaxesr6   r7   Úfigsizer$   Úlayoutr‰   r‹   rŽ   )r‰   r‹   F)Úindexrx   rf   r   zBoxplot grouped by ç333333Ã?çÍÌÌÌÌÌì?çš™™™™™¹?çš™™™™™É?©ZbottomÚtoprp   ÚrightZwspace)ÚgroupbyrY   ÚlistÚtupleÚ_get_numeric_datar{   Ú
differencer€   r   rX   r   rl   rw   Úziprz   Úappendr˜   rr   rs   Zsuptitler   )Zplotfr1   r{   rv   r—   r˜   rš   r$   r›   r-   r2   Úgroupedr™   Úfigr!   Z_axesr‰   r‹   Z	ax_valuesr�   ro   Zgp_colÚkeysr~   Zre_plotfr�   Zbyliner   r   r   Ú_grouped_plot_by_columnó   sF    


ú
	



r®   Úint)Úrotr˜   c
              
     s€  dd l m} ˆtjkrtdƒ‚t| tjƒr8|  d¡} d}‡fdd„}ddœ‡ fdd	„‰d
dœ‡‡‡‡fdd„}|ƒ ‰ |d kr‚d }nt|t	t
fƒr–|}n|g}|d k	rÈt|| f||||||ˆdœˆ—Ž}n´ˆd krÔd‰|d k	rätdƒ‚|d k�r |d k	rþd|ini }| |¡� | ¡ }W 5 Q R X |  ¡ } t| jƒ}|dk�rDtdƒ‚|d k�rV| j}n| | } ||| jj|fˆŽ}| |¡ |S )Nr   z,return_type must be {'axes', 'dict', 'both'}Úxc                    sª   t dd�} t | dddg¡} t | d¡} ˆ  dd ¡}|r¦t|ƒrœddd	d
g}tt|tdƒƒƒ}| 	¡ D ]2\}}||kr„|| || < qft
d|› d|› �ƒ‚qfn
|  |¡ | S )NrS   )rT   r   r9   ÚkrK   rM   rN   rO   rP   é   rQ   rR   )r   r?   Ztakerª   rX   r
   r"   r©   r   Úitemsr/   Úfill)r�   r`   r^   Zkey_to_indexr_   Úvalue)rH   r   r   rd   G  s     
ÿ
zboxplot.<locals>._get_colorsr,   r   c                   s„   |  d¡s t| d ˆ d dd� |  d¡s@t| d ˆ d dd� |  d¡s`t| d	 ˆ d
 dd� |  d¡s€t| d ˆ d dd� d S )Nre   rM   r   rf   rg   ri   rN   rj   rO   r9   rk   rP   rS   )rl   r   )rI   rH   )r`   r   r   rm   d  s    



zboxplot.<locals>.maybe_color_bpr   )r$   c                   s8  |  dd ¡|  dd ¡ }}|r,| t|ƒ¡ |r>| t|ƒ¡ dd„ | D ƒ} dd„ |D ƒ}|j|f|Ž}ˆ d k	r~|jdˆ d� | dd	¡}|r–| ¡ n| ¡ }t	|ƒt	| ƒkrÜt
t	|ƒt	| ƒƒ\}	}
|
d
ksÔt|
ƒ‚| |	9 } |rð|j| ˆd� n|j| ˆd� ˆ|f|Ž ˆdk�r|S ˆdk�r0tj||d�S |S d S )Nr‰   r‹   c                 S  s   g | ]}t |ƒ‘qS r   r   )r;   r±   r   r   r   r=   x  s     z/boxplot.<locals>.plot_group.<locals>.<listcomp>c                 S  s   g | ]}t jt|ƒtd �‘qS )rn   )r?   Zasarrayr   rt   r:   r   r   r   r=   y  s     r#   )ÚaxisZ	labelsizerŽ   Tr   )Zrotationr"   rB   )rX   rŠ   r   rŒ   rD   Ztick_paramsrl   Z
get_xticksZ
get_yticksr€   ÚdivmodÚAssertionErrorr†   r‡   r   r*   )r­   r~   r$   rH   r‰   r‹   rI   Zis_verticalZticksr�   Ú	remainder)Úfontsizerm   r-   r°   r   r   Ú
plot_groupp  s2    

zboxplot.<locals>.plot_group)r{   rv   r˜   rš   r$   r›   r-   r!   z7The 'layout' keyword is not supported when 'by' is Nonezfigure.figsizez;boxplot method requires numerical columns, nothing to plot.)Úmatplotlib.pyplotÚpyplotr   r.   r/   rY   rr   rs   Zto_framer¥   r¦   r®   Z
rc_contextZgcar§   r€   r{   r~   ry   r˜   )r1   Úcolumnrv   r$   r»   r°   r˜   rš   r›   r-   rH   Úpltrd   r¼   r{   r�   Úrcr™   r   )r`   r»   rH   rm   r-   r°   r   rD   0  sd    

#þ÷
ö


ÿ

rD   c
                 K  s<   dd l m} t| f|||||||||	dœ	|
—Ž}| ¡  |S )Nr   )	r¿   rv   r$   r»   r˜   r°   rš   r›   r-   )r½   r¾   rD   Zdraw_if_interactive)r   r¿   rv   r$   r»   r°   r˜   rš   r›   r-   rH   rÀ   r   r   r   Úboxplot_frameÆ  s$    ÿöõrÂ   )r4   r°   r˜   r6   r7   c              
   K  sF  |dkr t | ƒ}t|d||	|
||d�\}}t|ƒ}tjtd�}t| |ƒD ]@\\}}}|jf |||||dœ|—Ž}| t	|ƒ¡ ||j
|< qHt|ddddd	d
� n¢t| Ž \}}| jdkrÈtj||dd�}n,t |ƒdkrì|d  |dd … ¡}n|d }|d k	�r"t |¡}tj ||g¡}t|jƒ}|jf |||||||dœ|—Ž}|S )NTF)r™   Zsqueezer$   r6   r7   rš   r›   rn   )r$   r¿   r»   r°   r˜   r�   rž   rŸ   r    r¡   r   rf   )r­   r·   )r¿   r»   r°   r˜   r$   rš   r›   )r€   r   r   rr   rs   rt   r©   rD   rz   r   Úlocr   r·   ÚconcatÚjoinÚcomZconvert_to_list_likeZ
MultiIndexZfrom_productr¥   r~   )r«   r4   r¿   r»   r°   r˜   r$   rš   r›   r6   r7   rH   r™   r¬   r!   rƒ   r_   ÚgroupÚdr­   ÚframesZdfZ	multi_keyr   r   r   Úboxplot_frame_groupbyæ  s`    ù
	    ÿÿ



ùø
rÊ   )NNTFNNNN)	NNNNr   TNNN)	NNNNr   TNNN)
TNNr   TNNNFT)0Ú
__future__r   Útypingr   r   r   r   rV   Zmatplotlib.artistr   Znumpyr?   Zpandas._typingr   Zpandas.util._exceptionsr	   Zpandas.core.dtypes.commonr
   Zpandas.core.dtypes.missingr   Zpandasrr   Zpandas.core.commonÚcoreÚcommonrÆ   Zpandas.io.formats.printingr   Z pandas.plotting._matplotlib.corer   r   Z#pandas.plotting._matplotlib.groupbyr   Z!pandas.plotting._matplotlib.styler   Z!pandas.plotting._matplotlib.toolsr   r   r   Zmatplotlib.axesr   Zmatplotlib.linesr   r   r®   rD   rÂ   rÊ   r   r   r   r   Ú<module>   sz    N        ö?         ö          ö"          õ