o
    iio                     @   s   d Z ddlZddlZddlZddlm  mZ ddl	m
Z
mZmZmZ ddlmZ ddlmZmZ zddlmZ W n eyF   dd ZY nw ejdd	 ZejG d
d deZejG dd deZejG dd deZdS )z Test cases for .hist method     N)	DataFrameIndexSeriesto_datetime)TestPlotBase_check_plot_works)mpl_ge_3_6_0c                   C   s   dS )NT r	   r	   r	   a/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/pandas/tests/plotting/test_hist_method.py<lambda>   s    r   c                   C   s   t jddS )Ntsname)tmmakeTimeSeriesr	   r	   r	   r
   r      s   r   c                   @   s   e Zd Zdd Zdd Zdd Zejjdd Z	d	d
 Z
dd Zdd Zejdg ddd Zejdddgdd Zejdddgdd Zdd Zejje dd ejd!d" Zejd#d$ ZdS )%TestSeriesPlotsc                 C   sv  t |j t |jdd t |jdd tjtdd t |j|jjd W d    n1 s.w   Y  tjtdd t |j|jjdd W d    n1 sOw   Y  | jd	d	\}}t |j|d
d t |j||d
d t |j|d
d t	  | jd	d\}\}}t |j||d
d t |j||d
d d}t
jt|d |j|j|d W d    d S 1 sw   Y  d S )NFgrid   
   figsizecheck_stacklevelby   )r   bins   T)axdefault_axes)r    figurer!   )r"   r!      )r"   r    r!   z`Cannot pass 'figure' when using the 'by' argument, since a new 'Figure' instance will be createdmatch)r   r"   )r   histr   assert_produces_warningUserWarningindexmonthpltsubplotsclosepytestraises
ValueError)selfr   figr    ax1ax2msgr	   r	   r
   test_hist_legacy"   s,   
"z TestSeriesPlots.test_hist_legacyc                 C   s<   t tjdd}|jddd d }t|jdksJ d S )Nr   r#   r   r   )r   nprandomrandnr&   lenpatches)r1   dfr    r	   r	   r
   test_hist_bins_legacy=   s   z%TestSeriesPlots.test_hist_bins_legacyc                 C   s   |}d}t jt|d |jjdd W d    n1 sw   Y  t jt|d |jjddgd W d    d S 1 s>w   Y  d S )Nz7The 'layout' keyword is not supported when 'by' is Noner$   r   r   layoutr   )r.   r/   r0   heightr&   )r1   hist_dfr=   r5   r	   r	   r
   test_hist_layoutB   s   "z TestSeriesPlots.test_hist_layoutc                 C   sn  |}t jtdd t|jj|jdd}W d    n1 sw   Y  | j|ddd t jtdd t|jj|jdd}W d    n1 sGw   Y  | j|ddd t jtdd t|jj|jd	d}W d    n1 spw   Y  | j|d
d	d t jtdd t|jj|jdd}W d    n1 sw   Y  | j|d
dd t jtdd t|jj|jdd}W d    n1 sw   Y  | j|d
dd t jtdd t|jj|jdd}W d    n1 sw   Y  | j|d
dd t jtdd t|jj|j	dd}W d    n	1 sw   Y  | j|ddd |jj|jddd}| j|d
ddd d S )NFr   r#   r   r   rA   r#   axes_numrA   )   )rI   r      r   rL   r#   rJ   r#   r#   )rI   r#   rJ   rL   r   rL   rI   rL   r#   )      )r   rA   r   rH   rA   r   )
r   r'   r(   r   rB   r&   gender_check_axes_shapecategory	classroomr1   rC   r=   axesr	   r	   r
   test_hist_layout_with_byK   s>   z(TestSeriesPlots.test_hist_layout_with_byc                 C   sp   ddl m}m} ttjd}ttjd}|d |  |d |  | }|j}t	|dks6J d S )Nr   )gcfsubplotr#   y   z   )
matplotlib.pyplotr\   r]   r   r8   r9   r:   r&   rZ   r;   )r1   r\   r]   xyr2   rZ   r	   r	   r
   test_hist_no_overlapq   s   z$TestSeriesPlots.test_hist_no_overlapc                 C   s.   |}|j j|jd}t| j dksJ d S )Nr   r   )rB   r&   rU   r;   r+   get_fignumsrY   r	   r	   r
   test_hist_by_no_extra_plots   s   z+TestSeriesPlots.test_hist_by_no_extra_plotsc                 C   sh   ddl m} | }| }|d}d}tjt|d |j||d W d    d S 1 s-w   Y  d S )Nr   )r"   o   z&passed axis not bound to passed figurer$   )r    r"   )pylabr"   add_subplotr.   r/   AssertionErrorr&   )r1   r   r"   fig1fig2r3   r5   r	   r	   r
   +test_plot_fails_when_ax_differs_from_figure   s   
"z;TestSeriesPlots.test_plot_fails_when_ax_differs_from_figurehisttype, expected)barT)
barstackedT)stepF)
stepfilledTc                 C   s0   t tjdd}|j|d}| j||d d S )Nr   r   histtypefilled)r   r8   r9   randintr&   _check_patches_all_filled)r1   rt   expectedserr    r	   r	   r
   test_histtype_argument   s   z&TestSeriesPlots.test_histtype_argumentz&by, expected_axes_num, expected_layout)Nr   r?   )br#   )r   r#   c                 C   sd   ddg ddg  }t tjd|dd}d|j_t|jdd|d	}| j|||d
 | 	|d d S )N   12   ar)   r   r|   T)r!   legendr   rG   )
r   r8   r9   r:   r)   r   r   r&   rV   _check_legend_labels)r1   r   expected_axes_numexpected_layoutr)   srZ   r	   r	   r
   test_hist_with_legend   s   z%TestSeriesPlots.test_hist_with_legendr   Nr|   c                 C   sv   ddg ddg  }t tjd|dd}d|j_tjtdd	 |j	d
|dd W d    d S 1 s4w   Y  d S )Nr}   r~   r   r   r   r   r|    Cannot use both legend and labelr$   Tc)r   r   label)
r   r8   r9   r:   r)   r   r.   r/   r0   r&   )r1   r   r)   r   r	   r	   r
   test_hist_with_legend_raises   s   "z,TestSeriesPlots.test_hist_with_legend_raisesc                 C   s   | j  \}}|jjd|d}t|jdksJ | |j d t	
  | j  \}}|jjd|d}| |j d t	
  | j  \}}|jjdd|d}t	
  d S )	Nr   r   r    	Frequency
horizontal)orientationr    leftT)alignstackedr    )r+   r,   plotr&   r;   r<   _check_text_labelsyaxis	get_labelr   r-   xaxis)r1   r   _r    r	   r	   r
   test_hist_kwargs   s   z TestSeriesPlots.test_hist_kwargszApi changed)reasonc                 C   s   | j  \}}|jjd|d}| j|dd | }| |dgt|  | }| |dgt|  t	|jj
 t	|jj | j  \}}|jj
d|d}| j|dd | }| |dgt|  | }| |dgt|  d S )NT)logyr    logr    )r+   r,   r   r&   _check_ax_scalesget_xticklabelsr   r;   get_yticklabelsr   kdedensity)r1   r   r   r    xlabelsylabelsr	   r	   r
   test_hist_kde   s    zTestSeriesPlots.test_hist_kdec                 C   s   | j  \}}|jjddd|d}| j|dd t|jdks!J | j|jdgd d | j  \}}|jjdd|d	}| j|dd |	 }t|d
ksOJ | |dg d S )NTr   r|   )r   r   colorr    r   r   )
facecolorsr)r   r   r    r   )
r+   r,   r   r&   r   r;   r<   _check_colorsr   	get_lines)r1   r   r   r    linesr	   r	   r
   test_hist_kde_color   s   z#TestSeriesPlots.test_hist_kde_color)__name__
__module____qualname__r6   r>   rD   r.   markslowr[   rc   re   rl   parametrizer{   r   r   r   xfailr   tdskip_if_no_scipyr   r   r	   r	   r	   r
   r       s4    	
%

	

	r   c                   @   s   e Zd Zejjdd Zdd Zdd Zdd Z	d	d
 Z
ejddg dfg dg dfgdd Zejdg ddd Zejdddgejdddgdd Zejdddgejdddgdd Zdd Zdd  Zd!d" ZdS )#TestDataFramePlotsc                    sN  ddl m  tjtdd t|j W d    n1 sw   Y  ttj	
dd}ttj	jdddtjd	|d< tjtdd t|jdd
}W d    n1 sSw   Y  | j|ddd |d  rhJ t|dg j ttj	
dd}t|j ttj	
dd}ttj	jdddtjd	|d< tjtdd t|jdd}W d    n1 sw   Y  | j|ddd tjtdd t|jddd W d    n1 sw   Y  tjtdd t|jdd W d    n1 sw   Y  tjtdd t|jdd W d    n	1 sw   Y  |d }d\}}d\}}|j||||d}| j|||||d d\}}d\}}|j||||d}| j|||||d t  |jdddd}	 fdd|	 D }
t|
d   d! t  |jdd"}	| j|	d#d$ t  tt |jd%d& W d    d S 1 sw   Y  d S )'Nr   	RectangleFr   d   r#        j+2Z     ReZsizedtyper   rI   rN   rG   r?   r   r   rQ   r@      T)sharexshareyr   r   r7         r   (   
xlabelsizexrot
ylabelsizeyrotrL   )
cumulativer   r   c                       g | ]	}t | r|qS r	   
isinstance.0ra   r   r	   r
   
<listcomp>8      z:TestDataFramePlots.test_hist_df_legacy.<locals>.<listcomp>rJ         ?)r   r   r   ro   )foo)matplotlib.patchesr   r   r'   r(   r   r&   r   r8   r9   r:   r   rw   int64rV   get_visible_check_ticks_propsr-   get_childrenassert_almost_equal
get_heightr   external_error_raisedAttributeError)r1   rC   r=   rZ   rz   xfyfr   r   r    rectsr	   r   r
   test_hist_df_legacy   s   


$z&TestDataFramePlots.test_hist_df_legacyc                 C   s   t tjdtjdddttjjdddtjdttjjdddtjdddd}|t}d	}t	j
t|d
 |  W d    d S 1 sIw   Y  d S )Nr   r   l     p/ l    @spc/ )r   T)utc)r   r|   r   dzDhist method requires numerical or datetime columns, nothing to plot.r$   )r   r8   r9   randrw   r   r   astypeobjectr.   r/   r0   r&   )r1   r=   df_or5   r	   r	   r
   *test_hist_non_numerical_or_datetime_raisesF  s*   




"z=TestDataFramePlots.test_hist_non_numerical_or_datetime_raisesc                 C   st  t tjdd}ttjjdddtjd|d< d ddddddddd	d	dd
d
ddd	ddddddddddf	}|D ]}|j|d d}|d }| j|d|d q@d}t	j
t|d |jdd W d    n1 sqw   Y  td}t	j
t|d |jdd W d    n1 sw   Y  d}t	j
t|d |jdd W d    d S 1 sw   Y  d S )Nr   r#   r   r   r   rN   )rA   expected_sizerK   rP   )rI   rI   rO   )rL   rJ   )rJ   r#   rM   rA   r@   r   rI   rG   z1Layout of 1x1 must be larger than required size 3r$   r?   z)Layout must be a tuple of (rows, columns))r   1At least one dimension of layout must be positiverJ   rJ   )r   r8   r9   r:   r   rw   r   r&   rV   r.   r/   r0   reescape)r1   r=   layout_to_expected_sizelayout_testrZ   ry   r5   r	   r	   r
   rD   _  sF   

"z#TestDataFramePlots.test_hist_layoutc                 C   sT   t tjdd}ttjjdddtjd|d< t|jdd | j	
  t  d S )Nr   r#   r   r   r   T)r!   )r   r8   r9   r:   r   rw   r   r   r&   r+   tight_layoutr   r-   )r1   r=   r	   r	   r
   test_tight_layout  s   	
z$TestDataFramePlots.test_tight_layoutc              	   C   s@   t g dg dd}t|jdddddd	d
}| j|d	d d S )Ng      ?      ?g333333?g?rI   )pigrabbitr   r   r   )lengthanimalTalwaysr   r   r   r   )r!   filterwarningscolumnr   r   r   r   )r   r   r&   r   )r1   r=   rZ   r	   r	   r
   test_hist_subplot_xrot  s   	z)TestDataFramePlots.test_hist_subplot_xrotzcolumn, expectedNwidthr   rB   )r   r  rB   c                    s\   t g dg dg ddg dd}t|jd|dd	  fd
dtdD }||ks,J d S )N)gffffff?皙?g333333?r  g?r   )rI   r   g333333@r#   r   r   )r   r   duckchickenhorse)r)   Tr   rI   )r!   r   rA   c                    s   g | ]
} d |f   qS )r   )	get_title)r   irZ   r	   r
   r     s    zGTestDataFramePlots.test_hist_column_order_unchanged.<locals>.<listcomp>rI   )r   r   r&   range)r1   r   ry   r=   resultr	   r	  r
    test_hist_column_order_unchanged  s   

z3TestDataFramePlots.test_hist_column_order_unchangedrm   rn   c                 C   s<   t tjjddddddgd}|j|d}| j||d	 d S )
Nr   r   r   r#   r   r   r|   columnsrs   ru   r   r8   r9   rw   r&   rx   r1   rt   ry   r=   r    r	   r	   r
   r{     s   z)TestDataFramePlots.test_histtype_argumentr   r   r   r|   c                 C   s   |d u r
|d ur
dnd}d|f}|pddg}|d ur|gd }t ddg ddg  dd	}ttjd
d|ddgd}t|jdd||d}| j|||d |d u rY|d u rY|d }t||D ]
\}	}
| 	|
|	 q^d S )Nr   r#   r   r|   r}   r~   r   r   r   r   r)   r  T)r!   r   r   r   rG   r   )
r   r   r8   r9   r:   r   r&   rV   zipr   )r1   r   r   r   r   expected_labelsr)   r=   rZ   expected_labelr    r	   r	   r
   r     s(   
z(TestDataFramePlots.test_hist_with_legendc                 C   s~   t ddg ddg  dd}ttjdd|dd	gd
}tjtdd |jd||dd W d    d S 1 s8w   Y  d S )Nr}   r~   r   r   r   r   r#   r   r|   r  r   r$   Tr   )r   r   r   r   )	r   r   r8   r9   r:   r.   r/   r0   r&   )r1   r   r   r)   r=   r	   r	   r
   r     s
   "z/TestDataFramePlots.test_hist_with_legend_raisesc                 C   sF   t tjdd}| j \}}|jjd|d}t|j	dks!J d S )Nr   r#   r   r   )
r   r8   r9   r:   r+   r,   r   r&   r;   r<   r1   r=   r   r    r	   r	   r
   test_hist_df_kwargs  s   z&TestDataFramePlots.test_hist_df_kwargsc                 C   s   t d ttjddg dd}W d    n1 sw   Y  ddgd |d	< | j \}}|jj	d|d
}t
|jdksBJ | j \}}|jj	|d}t
|jdksYJ d S )Nr   r   rL   )ABCDr  ra   rb   r   Er   r   )r    r   )r   
RNGContextr   r8   r9   r:   r+   r,   r   r&   r;   r<   r  r	   r	   r
   test_hist_df_with_nonnumerics  s   z0TestDataFramePlots.test_hist_df_with_nonnumericsc                 C   s  t tjddtdd}| j \}}|d jjd|d}|d jj|ddd	 | j	|dd
gd |
  s:J |j
  sCJ t  | j \}}|d jjdd|d}|d jj|ddd	 | j	|jdd
gd |j
  rwJ |
  sJ t  | j \}}|d jjdd|d}|d jj|dd | j	|jddgd |j
  sJ |
  sJ t  d S )Nr   rL   abcdr  r   T)r   r    r|   )r    r   secondary_yz	b (right))labels)r   r!  r    z	a (right))r    r   )r   r8   r9   r:   listr+   r,   r   r&   r   	get_yaxisr   right_axr   r-   left_axr  r	   r	   r
   test_hist_secondary_legend  s,   z-TestDataFramePlots.test_hist_secondary_legend)r   r   r   r.   r   r   r   r   rD   r   r   r   r  r{   r   r   r  r  r'  r	   r	   r	   r
   r      s8    
U*


	r   c                   @   sf   e Zd Zdd Zdd Zejjdd Zdd Z	d	d
 Z
dd Zdd Zejdg ddd ZdS )TestDataFrameGroupByPlotsc                    s$  ddl m  ddlm} ttjdddgd}ttjj	dd	dtj
d
|d< tj	ddd|d< dgd |d< ||j|jd}| j|ddd t  |j|jd}| j|ddd t  |jddd}| j|ddd | j|dd t  d\}}d\}}||j|jdd||||dd	}| D ]} fdd| D }	|	d  }
t|
d q| j|||||d t  ||j|jdd }| j|d!d" t  tt ||j|jd#d$ W d    n1 sw   Y  d%}tjt|d& |jdd'd( W d    d S 1 sw   Y  d S ))Nr   r   )_grouped_histi  r   r  r  r   r   r   r  rL   r  Xr  r   rN   rG   r   )r   rotr?   r   r   r   T)r   r   r   r   r   r   r   r   c                    r   r	   r   r   r   r	   r
   r   k  r   zFTestDataFrameGroupByPlots.test_grouped_hist_legacy.<locals>.<listcomp>rJ   r   r   )r   r   r   r   ro   )r   r   z$Specify figure size by tuple insteadr$   default)r   r   )r   r    pandas.plotting._matplotlib.histr)  r   r8   r9   r:   r   rw   r   r  r  rV   r   r-   r&   r   ravelr   r   r   r   r   r   r.   r/   r0   )r1   r)  r=   rZ   r   r   r   r   r    r   rB   r5   r	   r   r
   test_grouped_hist_legacy;  sl   
$z2TestDataFrameGroupByPlots.test_grouped_hist_legacyc                 C   s   d}t tjjdd|d}t tjjdd|d}td tjjddg|d}W d    n1 s2w   Y  t|||d	}|d
}|	 }t
|dksPJ t
| j dks[J t  d S )Nr      r   r  <   *   r   r   )rB   weightrU   rU   r#   )r   r8   r9   normalr   r  choicer   groupbyr&   r;   r+   rd   r-   )r1   nr3  rB   
gender_intdf_intgbrZ   r	   r	   r
   test_grouped_hist_legacy2  s   
z3TestDataFrameGroupByPlots.test_grouped_hist_legacy2c                 C   s  |}d}t jt|d |jd|jdd W d    n1 sw   Y  d}t jt|d |jd|jdd W d    n1 sAw   Y  d	}t jt|d |jd|jd
d W d    n1 scw   Y  tjtdd t	|jd|jdd}W d    n1 sw   Y  | j
|ddd tjtdd t	|jd|jdd}W d    n1 sw   Y  | j
|ddd |jd|jdd}| j
|ddd |jd|jdd}| j
|ddd |jd|jddd}| j
|dddd t  tjtdd t	|jdddd}W d    n	1 sw   Y  | j
|ddd tjtdd t	|jdd}W d    n	1 s6w   Y  | j
|ddd |jddd}| j
|ddd |jg dd }| j
|ddd d S )!Nz1Layout of 1x1 must be larger than required size 2r$   r3  r?   )r   r   rA   z1Layout of 1x3 must be larger than required size 4rB   r  r   r   Fr   rE   r#   rG   rM   rK   rL   )rJ   r   rQ   )rR   r   )r   r   rA   r   rT   rX   rN   rI   r   rU   )rI   r   rF   rB   r3  rW   )r   )r.   r/   r0   r&   rU   rW   r   r'   r(   r   rV   r-   )r1   rC   r=   r5   rZ   r	   r	   r
   test_grouped_hist_layout  s\   
z2TestDataFrameGroupByPlots.test_grouped_hist_layoutc                 C   s   |}| j dd\}}|jg d|d d}| j|ddd t||d  |d j|u s/J |jd|d	 d
}| j|ddd t||d	  |d j|u sQJ | j dd\}}d}tjt	|d |jd|d}W d    d S 1 svw   Y  d S )Nr#   rI   r<  r   )r   r    r  rG   rX   r   )r   r    z@The number of passed axes must be 1, the same as the output plotr$   rB   )
r+   r,   r&   rV   r   assert_numpy_array_equalr"   r.   r/   r0   )r1   rC   r=   r2   rZ   returnedr5   r	   r	   r
   test_grouped_hist_multiple_axes  s   "z9TestDataFrameGroupByPlots.test_grouped_hist_multiple_axesc                 C   v   |}|j d|jdd\}}| |||sJ | |||s#J | |||r.J | |||r9J d S )NrB   T)r   r   r   r&   rU   
get_x_axisjoined
get_y_axisr1   rC   r=   r3   r4   r	   r	   r
   test_axis_share_x  s   z+TestDataFrameGroupByPlots.test_axis_share_xc                 C   rA  )NrB   T)r   r   r   )r&   rU   rE  rD  rC  rF  r	   r	   r
   test_axis_share_y  s   z+TestDataFrameGroupByPlots.test_axis_share_yc                 C   sx   |}|j d|jddd\}}| |||sJ | |||s$J | |||s/J | |||s:J d S )NrB   T)r   r   r   r   rB  rF  r	   r	   r
   test_axis_share_xy  s   z,TestDataFrameGroupByPlots.test_axis_share_xyrm   rn   c                 C   s>   t tjjddddddgd}|jd|d}| j||d	 d S )
Nr   r   r  r  r   r|   r  )r   rt   ru   r  r  r	   r	   r
   r{     s   z0TestDataFrameGroupByPlots.test_histtype_argumentN)r   r   r   r/  r;  r.   r   r   r=  r@  rG  rH  rI  r   r{   r	   r	   r	   r
   r(  9  s    E
6	r(  )__doc__r   numpyr8   r.   pandas.util._test_decoratorsutil_test_decoratorsr   pandasr   r   r   r   pandas._testing_testingr   pandas.tests.plotting.commonr   r   "pandas.plotting._matplotlib.compatr   ImportErrorfixturer   skip_if_no_mplr   r   r(  r	   r	   r	   r
   <module>   s0    
 N  L