U
    NZffj                     @   s   d Z ddlZddlZddlZddlZddlmZ ddlm	Z	 ddl
m  mZ ddlZddlmZmZmZ ddlmZ ddlmZ ddlmZ ejG dd	 d	eZdS )
z Test cases for DataFrame.plot     N)is_platform_linux)np_version_gte1p24)	DataFrameSeries
date_range)TestPlotBase)pprint_thingc                   @   s  e Zd Zejjdd Zdd Zdd Zejj	ddd	d
 Z
dd Zejdi dddfddidddfgdd Zejjdd Zdd Zdd Zdd Zdd Zejj	eoe dd d!d"d# Zejj	eoe dd d!d$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zejd.d/d0d1d2d3d4dd5gfgejd6d7d8d9gd:d; Zejd<d9d=d>d9d=d?d@dAd=d>dAd=d?d@d9d d>d9d d?d@dAd d>dAd d?d@d9d=dBd9d=d?dCdAd=dBdAd=d?dCd9d=dDdEd9d=d?dDdFdAd=dDdEdAd=d?dDdFd9d dDdEd9d d?dDdFdAd dDdEdAd d?dDdFd9d=dDdGd9d=d?dDdHdAd=dDdGdAd=d?dDdHgdIdJ Zejd<d9d d>d9d=d>dAd d>dAd=d>d9d=dBdAd=dBgdKdL Zejd<d9d d>d9d=d>dAd d>dAd=d>d9d=dBdAd=dBgdMdN ZejdOddPgdQdR ZdSdT Z dYdWdXZ!d3S )ZTestDataFramePlotsSubplotsc                 C   s  t tjddttjd d d}dD ]}|j|dddd}| j|ddd |j	d	ks^t
t||jD ]\}}| j|t|gd
 qj|d d D ]b}| |j | j| dd |dkr| j|jdddd | j|j dd | |  q| |d j | |d   | |d jdd | |d j  | |d   |j|ddd}|D ]P}| |j | |  | |jdd | |j  | |  qj|j|ddd}|D ]}| d kst
qq(d S )N
      index)barbarhlineareaT)kindsubplotssharexlegendr      Zaxes_numlayoutr   labelsFZvisibler   minorr   r   r   )r   r   r   )r   nprandomrandliststringascii_lettersplot_check_axes_shapeshapeAssertionErrorzipcolumns_check_legend_labelsr   _check_visiblexaxisget_xticklabels	get_labelget_yticklabelsZ
get_legend)selfdfr   axesaxcolumn r:   S/tmp/pip-unpacked-wheel-nbcvw55c/pandas/tests/plotting/frame/test_frame_subplots.pytest_subplots   s:    $
z(TestDataFramePlotsSubplots.test_subplotsc                 C   s  t dddd}ttjdd|d}dD ]t}|j|ddd	}| j|dd
d |d d D ]Z}| |j | j|	 dd | j|j	dddd | j|j
 dd | |  qZ| |d j | |d 	  | |d j	dd | |d j
  | |d   | j|dd |j|ddddd}|D ]b}| |j | |	  | |j	dd | |j
  | |  | j|dddd q:q(d S )N
2014-07-01Mr
   startfreqperiodsr   r   )r   r   Tr"   r   r   r   Fr   r   r!   r   )xrot-      )r   r   r   Zrotfontsize)Z
xlabelsizerC   Z
ylabelsize)r   r   r#   r$   r%   r)   r*   r0   r1   r2   r3   r4   Z_check_ticks_props)r5   idxr6   r   r7   r8   r:   r:   r;   test_subplots_timeseriesB   s2    
z3TestDataFramePlotsSubplots.test_subplots_timeseriesc           	   	   C   s  t dddgtjdddtjdddtjdd	dgtd
tdtdgtjd
ddtjdddtjdddgdddgd}t|}ddddg}|D ]>}|j|d}| d  d }|| j	}||k
 stqd}tjt|d |jdd W 5 Q R X d S )Nr         is)unitr
   mh2017-08-01 00:00:002017-08-01 02:00:002017-08-02 00:00:00TutcZThisZshouldZfail)numeric	timedeltadatetime_no_tzdatetime_all_tztextrT   rU   rV   rW   )yr   zno numeric data to plotmatchrX   )r#   arraypdZ	Timedeltato_datetimer   r)   	get_linesget_datavaluesallr,   pytestraises	TypeError)	r5   datatestdataZy_colscolr8   resultexpectedmsgr:   r:   r;   test_subplots_timeseries_y_axisb   s2    
z:TestDataFramePlotsSubplots.test_subplots_timeseries_y_axisz6not support for period, categorical, datetime_mixed_tz)reasonc                 C   s  t dddgtjdddtjdddtjdddgtjd	d
dgdd
d	gddtjdddtdtdgd}t|}|jddd}| d 	 d |d j
k st|jddd}| d 	 d |d j
k st|jddd}| d 	 d |d j
k stdS )a  
        This test will fail for:
            period:
                since period isn't yet implemented in ``select_dtypes``
                and because it will need a custom value converter +
                tick formatter (as was done for x-axis plots)

            categorical:
                 because it will need a custom value converter +
                 tick formatter (also doesn't work for x-axis, as of now)

            datetime_mixed_tz:
                because of the way how pandas handles ``Series`` of
                ``datetime`` objects with different timezone,
                generally converting ``datetime`` objects in a tz-aware
                form could help with this problem
        r   rI   rJ   rO   H)rA   z2017-08-01 02:00rQ   cbaF)
categoriesZorderedTrR   rP   )rT   periodcategoricaldatetime_mixed_tzrT   rs   )xrY   r   rt   ru   N)r#   r\   r]   ZPeriodZCategoricalr^   r   r)   r_   r`   ra   rb   r,   )r5   rf   rg   Z	ax_periodZax_categoricalZax_datetime_mixed_tzr:   r:   r;   -test_subplots_timeseries_y_axis_not_supported   s:      

zHTestDataFramePlotsSubplots.test_subplots_timeseries_y_axis_not_supportedc              	   C   s  t tjddttjd d d}|jddd}| j|ddd |j	dksPt
|jddd}| j|ddd |j	dks|t
|jdd	d}| j|ddd |j	dkst
|jdd
d}| j|dd
d |j	d
kst
|jddd}| j|dd
d |j	d
kst
|jddd}| j|ddd |j	dks0t
d}tjt|d |jd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   r   TrI   rI   r   r   r   r!   rI   rI   r!   )r      )r!   r|   )r|   r!   )r|   r   z1Layout of 1x1 must be larger than required size 3rZ   r   r   z1At least one dimension of layout must be positive)r!   r!   )r   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   rc   rd   
ValueError)r5   r6   r7   rk   r:   r:   r;   !test_subplots_layout_multi_column   s2    $z<TestDataFramePlotsSubplots.test_subplots_layout_multi_columnz:kwargs, expected_axes_num, expected_layout, expected_shaper   r}   r   r   )r   r   c                 C   sZ   t tjddttjd d d}|jf ddi|}| j|||d |j	|ksVt
d S )Nr
   r   r   r   Tr   )r   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   )r5   kwargsZexpected_axes_numZexpected_layoutZexpected_shaper6   r7   r:   r:   r;   "test_subplots_layout_single_column   s    $z=TestDataFramePlotsSubplots.test_subplots_layout_single_columnc              	   C   sf   t d R ttjdd}|jddd ttjddtdddd}|jddd W 5 Q R X d S )	Nd   r|   T)r   rI   ry   1/1/2000rB   r   )tmassert_produces_warningr   r#   r$   randnr)   r   r5   r6   r:   r:   r;   test_subplots_warnings   s     
z1TestDataFramePlotsSubplots.test_subplots_warningsc              	   C   sx  | j dd\}}ttjddttjd d d}|j	d|d ddd}| j
|dd	d
 |jdksjt|d j|ks|t|j	d|d ddd}| j
|dd	d
 |jdkst|d j|kst| j
|ddd
 t  d}tjt|d& | j dd\}}|j	d|d W 5 Q R X | j dd\}}t  tdt ttjddttjd d d}|j	d|dddd}| j
|ddd
 |jdkst|j	d|dddd}| j
|ddd
 |jdkst|j	d|dddd}W 5 Q R X | j
|ddd
 |jdks
t| j dd\}}ttjddttjd d d}|j	d|gddd}| j
|ddd
 |jdksttd S )NrI   r   r
   r   Tr   F)r   r8   r   sharey)r   r   r   r   r      )rI   r   z@The number of passed axes must be 3, the same as the output plotrZ   r   r8   ignorer|   )rI   r   )r   r8   r   r   r   rx   )r|   r{   rz   r}   r   )pltr   r   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   figurer   closerc   rd   r~   warningscatch_warningssimplefilterUserWarning)r5   figr7   r6   returnedrk   r:   r:   r;   test_subplots_multiple_axes   sh    $
$            $z6TestDataFramePlotsSubplots.test_subplots_multiple_axesc                 C   sX  | j jddddd\}}| j jddddd ttjdd	td
dddd}t|	 D ]\}}|| j
|dd qX|dd 	 D ]}| j| dd q|d 	 D ]}| j| dd q|dddgdgf 	 D ]}| j| dd q|dddgdgf 	 D ]}| j| dd q|dddgdgf 	 D ]}| j| dd q:d S )Nr   T)r   r   g?gffffff?g333333?)leftrightZhspaceZwspacer
   	   r=   r>   r?   r   rJ   )r8   rF   r   r!   Fr   r   rI   )r   r   Zsubplots_adjustr   r#   r$   r   r   	enumerateravelr)   r0   r2   r4   )r5   r   r7   r6   ir8   r:   r:   r;   test_subplots_ts_share_axes7  s$    z6TestDataFramePlotsSubplots.test_subplots_ts_share_axesc                 C   s   ddddgddddgddddgd	}t |td
dd}|ddg jdd}|d j|d dd | j|d  dd | j|d  dd | D ]}| j| dd qd S )N      ?g       @g      @g      @rJ   r   r   r|   )ABCz
2014 10 11z
2014 10 14r   r   r   Tr   r   r   )r8   secondary_yFr   )r   r   r)   r0   r2   r   r4   )r5   dr6   r7   r8   r:   r:   r;   'test_subplots_sharex_axes_existing_axesT  s    $zBTestDataFramePlotsSubplots.test_subplots_sharex_axes_existing_axesc                 C   s   t tjddtdd}|jdd}|D ]&}| j|dgd t|jdks*t	q*t
  |jddd	}|D ]&}| j|dgd t|jdkslt	qlt
  |jdd
}| j|dgd d t|jdkst	t|jjdkst	d S )NrJ   Zaaaaar.   Tr   rq   r   r   )r   r   )r   z	a (right)r   )r   r#   r$   r%   r&   r)   r/   lenlinesr,   r   r   Zright_ax)r5   r6   r7   r8   r:   r:   r;   test_subplots_dup_columnsa  s    z4TestDataFramePlotsSubplots.test_subplots_dup_columnszWeird rounding problemsF)rm   strictc                 C   s^   t ddddg}tdgd ttddd	tdd
}|jjddd}t|j	
 | d S )N皙?r         $@r   r   rJ   r   r   r   r   r   T)gridlog)r#   r\   r   r&   ranger)   r   r   assert_numpy_array_equalyaxisget_ticklocs)r5   rj   r6   r8   r:   r:   r;   test_bar_log_no_subplotsv  s    &z3TestDataFramePlotsSubplots.test_bar_log_no_subplotsc                 C   sn   t ddddddg}ttddgtdd	ggjjd
d
d}t|d j	 | t|d j	 | d S )Nr   r   r   g      Y@g     @@g     @   i,  i  T)r   r   r   r   )
r#   r\   r   r   r)   r   r   r   r   r   )r5   rj   r8   r:   r:   r;   test_bar_log_subplots  s     z0TestDataFramePlotsSubplots.test_bar_log_subplotsc                 C   sl   |}|j jdd}t|ts t| j|d dddgd dD ],}|j j|dd}| j||dddgd	d
 q:d S )NTr   heightZweightcategory)expected_keys)dictr7   Zboth)return_typer   F)r   Zcheck_ax_title)r)   Zbox
isinstancer   r,   Z_check_box_return_type)r5   Zhist_dfr6   ri   tr   r:   r:   r;   !test_boxplot_subplots_return_type  s       z<TestDataFramePlotsSubplots.test_boxplot_subplots_return_typec              	   C   s<  dd l m} ttjddtdddtdd}|jddd	d
\}}|j	d	|d}|D ](}t
|jdkslt| j| d	d qV| j|d  dd | j|d jd	ddd | j|d  d	d | j|d jd	dd	d t  |dd\}}tt |j	d	|d	d}W 5 Q R X |D ],}t
|jdks:t| j| d	d q"| j|d  dd | j|d jd	ddd | j|d  d	d | j|d jd	dd	d t  |dd\}}|j	d	|d}|D ]T}t
|jdkst| j| d	d | j| d	d | j|jd	dd	d qt  d S )Nr   r
   rI   r   r   ZAB)r   r.   r   T)r   r   r   Fr   )r   r8   r   )Zmatplotlib.pyplotZpyplotr   r#   r$   r   r   r&   r   r)   r   r   r,   r0   r4   r2   r   r   r   r   )r5   r   r6   r   r7   r8   r:   r:   r;   $test_df_subplots_patterns_minorticks  sF    
z?TestDataFramePlotsSubplots.test_df_subplots_patterns_minorticksc                 C   s   t tjdd}tj|jdd df< tj|jd ddf< | jdd\}}|jj	|ddd t
d	dd
}t
ddd
}t|d  | t|d  | d S )Nr
   rI   rJ   r   r   TF)r8   r   r   g      @      ?      )r   r#   r$   r%   nanZilocr   r   r)   r   aranger   r   Z
get_xticks)r5   r6   ZfigsZaxsZexpected_ax1Zexpected_ax2r:   r:   r;   test_subplots_sharex_false  s    z5TestDataFramePlotsSubplots.test_subplots_sharex_falsec              
   C   s   t ddd}ttjdd|d}i }t| jjdr<d|d< | jjd|\}}t	
d : |j|d
 d t	jdd}| j| W 5 Q R X W 5 Q R X d S )Nnowr
   )r@   rB   r   r   Zget_constrained_layoutTZconstrained_layoutrI   r   )r8   )Zreturn_filelike)rI   )r   r   r#   r$   r%   hasattrr   ZFigurer   r   r   r)   Zensure_cleanZsavefig)r5   rG   r6   r   r   r7   pathr:   r:   r;    test_subplots_constrained_layout  s    z;TestDataFramePlotsSubplots.test_subplots_constrained_layoutz index_name, old_label, new_label)N new)oldr   r   )Nr   r   )Nr   r   Nr   rI   r   r   r   r   c                    s   t ddgddggddgd}||j_|j|dd}td	d
 |D sHttfdd
|D sbt|j|  dd}t fdd
|D stt fdd
|D std S )Nr   rI   rJ   zType AzType Br   Tr   r   c                 s   s   | ]}|  d kV  qdS )r   N)
get_ylabel.0r8   r:   r:   r;   	<genexpr>  s     zSTestDataFramePlotsSubplots.test_xlabel_ylabel_dataframe_subplots.<locals>.<genexpr>c                 3   s   | ]}|   kV  qd S N)
get_xlabelr   )	old_labelr:   r;   r     s     )r   ZylabelZxlabelr   c                 3   s   | ]}|  t kV  qd S r   )r   strr   	new_labelr:   r;   r     s     c                 3   s   | ]}|  t kV  qd S r   )r   r   r   r   r:   r;   r     s     )r   r   namer)   rb   r,   )r5   r   Z
index_namer   r   r6   r7   r:   )r   r   r;   %test_xlabel_ylabel_dataframe_subplots  s    z@TestDataFramePlotsSubplots.test_xlabel_ylabel_dataframe_subplotsr   T)r   stacked?r   r   widthr   r   r   r   r   edge)r   r   align)r   r   r   r   )r   r   r   )r   r   r   r   c                 C   s6   t dgd ttddtdd}| j|f| d S )Nr   rJ   r   r   )r   r&   r   _check_bar_alignmentr5   r   r6   r:   r:   r;   test_bar_align_multiple_columns  s    #$z:TestDataFramePlotsSubplots.test_bar_align_multiple_columnsc                 C   s"   t tjd}| j|f| d S )NrJ   r   r#   r$   r   r   r   r:   r:   r;   test_bar_align_single_column4  s    z7TestDataFramePlotsSubplots.test_bar_align_single_columnc                 C   s.   t tjdd}| j|fddd| d S )NrJ   r   g?)r   positionr   r   r:   r:   r;   test_bar_barwidth_positionC  s    z5TestDataFramePlotsSubplots.test_bar_barwidth_positionwr   c              
   C   s   t tjdd}|jjd|d}|j }t	|t
dddddg | d	ksXt|jd  d
ksnt|jd  dkstd S )NrJ   T)r   r   r   r   rI   r   r|   )g      g      @r   r!   g      @)r   r#   r$   r   r)   r   r1   r   r   r   r\   get_xlimr,   patchesget_x)r5   r   r6   r8   Zticksr:   r:   r;   test_bar_barwidth_position_intR  s    
z9TestDataFramePlotsSubplots.test_bar_barwidth_position_intc                 C   sp   t tjdd}| j|dddd | j|dddd | j|dddd | j|dddd | j|dddd d S )	NrJ   r   Tr   r   r   Fr   r   r   r:   r:   r;   &test_bar_barwidth_position_int_width_1^  s    zATestDataFramePlotsSubplots.test_bar_barwidth_position_int_width_1centerr   c              	   C   s:  |j ||||||dd}| |}|D ]}	|dkrp|	j}
|	 \}}tdd |	jD }tdd |	jD }nH|dkr|	j}
|	 \}}tdd |	jD }td	d |	jD }nt	t
||d
  t
||d
  |	jd }|dkr|dks|dkr| }|| |  }n|dkrT|dkrT| | t|j |  }| }n||dkr|dksr|dkr| | |  }| }n@|dkr|dkr| | t|j |  }| }nt	|
 tt|k st|dkrt
|
 d | q&|dkr0t
|
 d | q&t	q&|S )NT)r   r   r   r   r   r   r   r   c                 s   s   | ]}|  V  qd S r   )r   r   pr:   r:   r;   r     s     zBTestDataFramePlotsSubplots._check_bar_alignment.<locals>.<genexpr>c                 s   s   | ]}|  |  V  qd S r   )r   	get_widthr   r:   r:   r;   r     s     r   c                 s   s   | ]}|  V  qd S r   )get_yr   r:   r:   r;   r     s     c                 s   s   | ]}|  |  V  qd S r   )r   
get_heightr   r:   r:   r;   r     s     g      ?r   Fr   r   )r)   Z_flatten_visibler1   r   minr   maxr   Zget_ylimr~   r   Zassert_almost_equalr   r   r   r.   r   r   r   r#   r   rb   r,   )r5   r6   r   r   r   r   r   r   r7   r8   ZaxisZax_minZax_maxZmin_edgeZmax_edger   r   r   r:   r:   r;   r   g  sZ    







 

z/TestDataFramePlotsSubplots._check_bar_alignment)r   FFr   r   r   )"__name__
__module____qualname__rc   markZslowr<   rH   rl   Zxfailrw   r   Zparametrizer   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r:   r:   r:   r;   r	      s   
& "
2%


;



.












!



      r	   )__doc__r'   r   Znumpyr#   rc   Zpandas.compatr   Zpandas.compat.numpyr   Zpandas.util._test_decoratorsutilZ_test_decoratorstdZpandasr]   r   r   r   Zpandas._testingZ_testingr   Zpandas.tests.plotting.commonr   Zpandas.io.formats.printingr   Zskip_if_no_mplr	   r:   r:   r:   r;   <module>   s   