U
    sVcio                     @   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k
r   dd ZY nX 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	   J/tmp/pip-unpacked-wheel-xj8nt62q/pandas/tests/plotting/test_hist_method.py<lambda>       r   c                   C   s   t jddS )Ntsname)tmZ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ddddg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   s8  t |j t |jdd t |jdd tjtdd t |j|jjd W 5 Q R X tjtdd t |j|jjdd W 5 Q R X | 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 5 Q R X d S )NFZgrid   
   figsizeZ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s8td S )Nr   r!   r   r   )r   nprandomrandnr$   lenpatchesAssertionError)r/   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 5 Q R X t jt|d |jjddgd W 5 Q R X d S )Nz7The 'layout' keyword is not supported when 'by' is Noner"   r   r   layoutr   )r,   r-   r.   heightr$   )r/   hist_dfr<   r3   r	   r	   r
   test_hist_layoutB   s    z TestSeriesPlots.test_hist_layoutc              	   C   s  |}t jtdd t|jj|jdd}W 5 Q R X | j|ddd t jtdd t|jj|jdd}W 5 Q R X | j|ddd t jtdd t|jj|jd	d}W 5 Q R X | j|d
d	d t jtdd t|jj|jdd}W 5 Q R X | j|d
dd t jtdd t|jj|jdd}W 5 Q R X | j|d
dd t jtdd t|jj|jdd}W 5 Q R X | j|d
dd t jtdd t|jj|j	dd}W 5 Q R X | j|ddd |jj|jddd}| j|d
ddd d S )NFr   r!   r   r   r@   r!   axes_numr@   )   )rH   r      r   rK   r!   rI   r!   r!   )rH   r!   rI   rK   r   rK   rH   rK   r!   )      )r   r@   r   rG   r@   r   )
r   r%   r&   r   rA   r$   gender_check_axes_shapecategory	classroomr/   rB   r<   axesr	   r	   r
   test_hist_layout_with_byK   s0    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slt
d S )Nr   )gcfsubplotr!   y   z   )Zmatplotlib.pyplotr[   r\   r   r6   r7   r8   r$   rY   r9   r;   )r/   r[   r\   xyr0   rY   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*td S )Nr   r   )rA   r$   rT   r9   r)   get_fignumsr;   rX   r	   r	   r
   test_hist_by_no_extra_plots   s    z+TestSeriesPlots.test_hist_by_no_extra_plotsc              	   C   sR   ddl m} | }| }|d}d}tjt|d |j||d W 5 Q R X d S )Nr   )r    o   z&passed axis not bound to passed figurer"   )r   r    )Zpylabr    Zadd_subplotr,   r-   r;   r$   )r/   r   r    Zfig1Zfig2r1   r3   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Z
barstackedTstepFZ
stepfilledTc                 C   s0   t tjdd}|j|d}| j||d d S )Nr   r   histtypeZfilled)r   r6   r7   randintr$   _check_patches_all_filled)r/   rn   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   ru   T)r   legendr   rF   )
r   r6   r7   r8   r'   r   r   r$   rU   _check_legend_labels)r/   r   expected_axes_numexpected_layoutr'   srY   r	   r	   r
   test_hist_with_legend   s    z%TestSeriesPlots.test_hist_with_legendr   Nru   c              	   C   s`   ddg ddg  }t tjd|dd}d|j_tjtdd	 |j	d
|dd W 5 Q R X d S )Nrv   rw   rx   ry   rz   r{   ru    Cannot use both legend and labelr"   Tc)r|   r   label)
r   r6   r7   r8   r'   r   r,   r-   r.   r$   )r/   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s0t| |j	 d t
  | j  \}}|jjd|d}| |j	 d t
  | j  \}}|jjdd|d}t
  d S )	Nr   r   r   Z	FrequencyZ
horizontal)Zorientationr   leftT)ZalignZstackedr   )r)   r*   plotr$   r9   r:   r;   _check_text_labelsyaxisZ	get_labelr   r+   Zxaxis)r/   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_scalesZget_xticklabelsr   r9   Zget_yticklabelsr   kdedensity)r/   r   r   r   ZxlabelsZ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sBt| j|jdgd d | j  \}}|jj	dd|d	}| j|dd |
 }t|d
kst| |dg d S )NTr   ru   )r   r   colorr   r   r   )Z
facecolorsr)r   r   r   r   )r)   r*   r   r$   r   r9   r:   r;   Z_check_colorsr   	get_lines)r/   r   r   r   linesr	   r	   r
   test_hist_kde_color   s    z#TestSeriesPlots.test_hist_kde_color)__name__
__module____qualname__r4   r=   rC   r,   markslowrZ   ra   rc   re   parametrizert   r   r   r   Zxfailr   tdZskip_if_no_scipyr   r   r	   r	   r	   r
   r       s<   	
%
	
 

	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dddgfdddgdddgfgdd Zejdddddg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              	      s  ddl m  tjtdd t|j W 5 Q R X 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 5 Q R X | j|ddd |d  rt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 5 Q R X | j|ddd tjtdd t|jddd W 5 Q R X tjtdd t|jdd W 5 Q R X tjtdd t|jdd W 5 Q R X |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 5 Q R X d S )'Nr   	RectangleFr   d   r!        j+2Z     ReZsizedtyper   rH   rM   rF   r>   r   r   rP   r?      T)sharexshareyr   r   r5         ry   (   
xlabelsizexrot
ylabelsizeyrotrK   )
cumulativer   r   c                    s   g | ]}t | r|qS r	   
isinstance.0r_   r   r	   r
   
<listcomp>8  s     
 z:TestDataFramePlots.test_hist_df_legacy.<locals>.<listcomp>rI         ?)r   r   r   rh   )foo)matplotlib.patchesr   r   r%   r&   r   r$   r   r6   r7   r8   r   rp   int64rU   get_visibler;   _check_ticks_propsr+   get_childrenassert_almost_equal
get_heightr   external_error_raisedAttributeError)r/   rB   r<   rY   rs   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 5 Q R X d S )Nr   r   l     p/ l    @spc/ )r   T)utc)rz   ru   r   dzDhist method requires numerical or datetime columns, nothing to plot.r"   )r   r6   r7   Zrandrp   r   r   Zastypeobjectr,   r-   r.   r$   )r/   r<   Zdf_or3   r	   r	   r
   *test_hist_non_numerical_or_datetime_raisesF  s4    
      
z=TestDataFramePlots.test_hist_non_numerical_or_datetime_raisesc                 C   s6  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 qd}t	j
t|d |jdd W 5 Q R X td}t	j
t|d |jdd W 5 Q R X d}t	j
t|d |jdd W 5 Q R X d S )Nr   r!   r   r   r   rM   )r@   expected_sizerJ   rO   )rH   rH   rN   )rK   rI   )rI   r!   rL   r@   r?   r   rH   rF   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rI   rI   )r   r6   r7   r8   r   rp   r   r$   rU   r,   r-   r.   reescape)r/   r<   Zlayout_to_expected_sizeZlayout_testrY   rr   r3   r	   r	   r
   rC   _  s@    

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   r6   r7   r8   r   rp   r   r   r$   r)   Ztight_layoutr   r+   )r/   r<   r	   r	   r
   test_tight_layout  s    	
z$TestDataFramePlots.test_tight_layoutc              	   C   sL   t dddddgdddddgd}t|jd	d
ddddd}| j|dd d S )N      ?      ?333333??rH   pigrabbit)lengthanimalTalwaysr   r   r   r   )r   filterwarningscolumnr   r   r   r   )r   r   r$   r   )r/   r<   rY   r	   r	   r
   test_hist_subplot_xrot  s    	z)TestDataFramePlots.test_hist_subplot_xrotzcolumn, expectedNwidthr   rA   c                    st   t dddddgddddd	gd	dd
ddgddddddgd}t|jd|dd  fddtd	D }||ksptd S )Ngffffff?g?g333333?g?r   r   r   r   rH   g333333@r!   r   )r   r   rA   r   r   ZduckZchickenZhorse)r'   Tr   rH   )r   r   r@   c                    s   g | ]} d |f   qS )r   )Z	get_title)r   irY   r	   r
   r     s     zGTestDataFramePlots.test_hist_column_order_unchanged.<locals>.<listcomp>)r   r   r$   ranger;   )r/   r   rr   r<   resultr	   r   r
    test_hist_column_order_unchanged  s    

z3TestDataFramePlots.test_hist_column_order_unchangedrf   rg   ri   rj   rl   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   rz   ru   columnsrm   ro   r   r6   r7   rp   r$   rq   r/   rn   rr   r<   r   r	   r	   r
   rt     s    z)TestDataFramePlots.test_histtype_argumentr   r   r   ru   c                 C   s   |d kr|d k	rdnd}d|f}|p*ddg}|d k	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 kr|d kr|d }t||D ]\}	}
| 	|
|	 qd S )Nr   r!   rz   ru   rv   rw   rx   r   r   ry   r'   r   T)r   r|   r   r   rF   r   )
r   r   r6   r7   r8   r   r$   rU   zipr}   )r/   r   r   r~   r   Zexpected_labelsr'   r<   rY   Zexpected_labelr   r	   r	   r
   r     s&    
z(TestDataFramePlots.test_hist_with_legendc              	   C   sh   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 5 Q R X d S )Nrv   rw   rx   r   r   ry   r!   rz   ru   r   r   r"   Tr   )r|   r   r   r   )	r   r   r6   r7   r8   r,   r-   r.   r$   )r/   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sBt
d S )Nr   r!   r   r   )r   r6   r7   r8   r)   r*   r   r$   r9   r:   r;   r/   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ddddgd}W 5 Q R X d	d
gd |d< | j \}}|jj	d|d}t
|jdkstt| j \}}|jj	|d}t
|jdkstd S )Nr   r   rK   ABCDr   r_   r`   r   Er   r   )r   r   )r   
RNGContextr   r6   r7   r8   r)   r*   r   r$   r9   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 |
  stt|j
  stt  | j \}}|d jjdd|d}|d jj|ddd	 | j	|jdd
gd |j
  rt|
  stt  | j \}}|d jjdd|d}|d jj|dd | j	|jddgd |j
  sft|
  sxtt  d S )Nry   rK   Zabcdr   rz   T)r|   r   ru   )r   r|   secondary_yz	b (right))labels)r|   r   r   z	a (right))r   r|   )r   r6   r7   r8   listr)   r*   r   r$   r}   Z	get_yaxisr   r;   Zright_axr   r+   Zleft_axr   r	   r	   r
   test_hist_secondary_legend  s,    z-TestDataFramePlots.test_hist_secondary_legend)r   r   r   r,   r   r   r   r   rC   r   r   r   r   rt   r   r   r   r   r   r	   r	   r	   r
   r      s>   
U*
	
r   c                   @   sj   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ddddg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 ]4} 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 5 Q R X d%}tjt|d& |jdd'd( W 5 Q R X d S ))Nr   r   )_grouped_histi  r   r   r   r   r   r   r   rK   r   Xr   r   rM   rF   ry   )r   Zrotr>   r   r   r   T)r   r   r   r   r   r   r   r   c                    s   g | ]}t | r|qS r	   r   r   r   r	   r
   r   k  s     
 zFTestDataFrameGroupByPlots.test_grouped_hist_legacy.<locals>.<listcomp>rI   r   r   )r   r   r   r   rh   )r   r   z$Specify figure size by tuple insteadr"   default)r   r   )r   r   Z pandas.plotting._matplotlib.histr  r   r6   r7   r8   r   rp   r   r   r   rU   r   r+   r$   r   Zravelr   r   r   r   r   r   r,   r-   r.   )r/   r  r<   rY   r   r   r   r   r   r   rA   r3   r	   r   r
   test_grouped_hist_legacy;  sp        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 5 Q R X t|||d	}|d
}|	 }t
|dkstt
| j dkstt  d S )Nr      r   r   <   *   r   r   )rA   weightrT   rT   r!   )r   r6   r7   normalr   r   choicer   groupbyr$   r9   r;   r)   rb   r+   )r/   nr  rA   Z
gender_intZdf_intgbrY   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 5 Q R X d}t jt|d |jd|jdd W 5 Q R X d	}t jt|d |jd|jd
d W 5 Q R X tjtdd t	|jd|jdd}W 5 Q R X | j
|ddd tjtdd t	|jd|jdd}W 5 Q R X | 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 5 Q R X | j
|ddd tjtdd t	|jdd}W 5 Q R X | j
|ddd |jddd}| j
|ddd |jdddgd }| j
|ddd d S )!Nz1Layout of 1x1 must be larger than required size 2r"   r  r>   )r   r   r@   z1Layout of 1x3 must be larger than required size 4rA   r   r   r   Fr   rD   r!   rF   rL   rJ   rK   )rI   r   rP   )rQ   r   )r   r   r@   r   rS   rW   rM   rH   r   rT   )rH   r   rE   rV   )r   )r,   r-   r.   r$   rT   rV   r   r%   r&   r   rU   r+   )r/   rB   r<   r3   rY   r	   r	   r
   test_grouped_hist_layout  s`             z2TestDataFrameGroupByPlots.test_grouped_hist_layoutc              	   C   s   |}| j dd\}}|jdddg|d d}| j|ddd	 t||d  |d j|ks`t|jd
|d d}| j|ddd	 t||d  |d j|kst| j dd\}}d}tj	t
|d |jd|d}W 5 Q R X d S )Nr!   rH   rA   r  rV   r   )r   r   r   rF   rW   r   )r   r   z@The number of passed axes must be 1, the same as the output plotr"   )r)   r*   r$   rU   r   Zassert_numpy_array_equalr    r;   r,   r-   r.   )r/   rB   r<   r0   rY   Zreturnedr3   r	   r	   r
   test_grouped_hist_multiple_axes  s    z9TestDataFrameGroupByPlots.test_grouped_hist_multiple_axesc                 C   sv   |}|j d|jdd\}}| |||s0t| |||sFt| |||r\t| |||rrtd S )NrA   T)r   r   r   r$   rT   
get_x_axisjoinedr;   
get_y_axisr/   rB   r<   r1   r2   r	   r	   r
   test_axis_share_x  s    z+TestDataFrameGroupByPlots.test_axis_share_xc                 C   sv   |}|j d|jdd\}}| |||s0t| |||sFt| |||r\t| |||rrtd S )NrA   T)r   r   r   )r$   rT   r  r  r;   r  r  r	   r	   r
   test_axis_share_y  s    z+TestDataFrameGroupByPlots.test_axis_share_yc                 C   sx   |}|j d|jddd\}}| |||s2t| |||sHt| |||s^t| |||sttd S )NrA   T)r   r   r   r   r  r  r	   r	   r
   test_axis_share_xy  s    z,TestDataFrameGroupByPlots.test_axis_share_xyrf   rg   ri   rj   rl   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   rz   ru   r   )r   rn   ro   r   r   r	   r	   r
   rt     s    z0TestDataFrameGroupByPlots.test_histtype_argumentN)r   r   r   r  r  r,   r   r   r  r  r  r  r  r   rt   r	   r	   r	   r
   r   9  s"   E
6	r   )__doc__r   Znumpyr6   r,   Zpandas.util._test_decoratorsutilZ_test_decoratorsr   Zpandasr   r   r   r   Zpandas._testingZ_testingr   Zpandas.tests.plotting.commonr   r   Z"pandas.plotting._matplotlib.compatr   ImportErrorZfixturer   Zskip_if_no_mplr   r   r   r	   r	   r	   r
   <module>   s,   
 N  L