B
    x\2                 @   s   d dl mZ d dlmZ d dlZd dlZd dlmZ d dl	m
Z
 d dlmZmZmZ dd Zdd	d
ZdddZd ddZdd Zdd Zdd Zdd Zdd Zd!ddZdS )"    )division)ceilN)range)is_list_like)ABCDataFrameABCIndexClass	ABCSeriesc             C   sX   y>x$|   D ]}|d || qW |  }|jdd W n tk
rR   Y nX d S )Nrightg?)Zbottom)get_xticklabelsZset_haZset_rotation
get_figureZsubplots_adjust	Exception)axZrotZlabelfig r   5lib/python3.7/site-packages/pandas/plotting/_tools.pyformat_date_labels   s    
r   c             K   sr   t |tr| }nt |tr ntd|dkr6|j}|dkrD|j}|j}ddl}|j	j	| f|||d|}|S )a  
    Helper function to convert DataFrame and Series to matplotlib.table

    Parameters
    ----------
    ax : Matplotlib axes object
    data : DataFrame or Series
        data for table contents
    kwargs : keywords, optional
        keyword arguments which passed to matplotlib.table.table.
        If `rowLabels` or `colLabels` is not specified, data index or column
        name will be used.

    Returns
    -------
    matplotlib table object
    z&Input data must be DataFrame or SeriesNr   )cellText	rowLabels	colLabels)

isinstancer   Zto_framer   
ValueErrorindexcolumnsvaluesZmatplotlib.tabletable)r   datar   r   kwargsr   Z
matplotlibr   r   r   r   r      s    


r   boxc       	      C   s~  |d k	rt |ttfr"t|dkr*td|\}}dd }|dkrh|dkrh|t| | |f }\}}nJ|dkr|dkr||t| | f }\}}n|dkr|dkrd}t||| | k rtdj||| d	|S |d
krdS |dkrd| fS |dkr| dfS ddddd}y||  S  tk
rx   d}x|d | k rL|d7 }q2W |d | | krl||d fS ||fS Y nX d S )N   z)Layout must be a tuple of (rows, columns)c             S   s   t t| S )N)intr   )xr   r   r   <lambda>L   s    z_get_layout.<locals>.<lambda>r   z1At least one dimension of layout must be positivezDLayout of {nrows}x{ncols} must be larger than required size {nplots})nrowsncolsnplotsZsingle)   r&   Z
horizontalr&   Zvertical)r&   r   )r   r   )r&   r         )r   tuplelistlenr   floatformatKeyError)	r%   layoutlayout_typer#   r$   Zceil_msgZlayoutskr   r   r   _get_layoutD   s@    
r3   FTc             K   s  ddl m}	 |dkri }|dkr.|	jf |}
nt|rt|}|dk	rRtdt |sZ|rjtjdtdd t|| kr|d 	 }
|
|fS t
d| |	 }
| dkr|r|
|fS |
t|fS ntjd	tdd |
  t| ||d
\}}|| }tj|td}|
j||df|}|r"||d< |r0||d< ||d< xTtd|D ]F}| }|| krjd|d< d|d< |
j|||d f|}|||< qDW | |krx || d D ]}|d qW t||| |||| |r|dkr|d }n||| }n|||}|
|fS )ax	  Create a figure with a set of subplots already made.

    This utility wrapper makes it convenient to create common layouts of
    subplots, including the enclosing figure object, in a single call.

    Keyword arguments:

    naxes : int
      Number of required axes. Exceeded axes are set invisible. Default is
      nrows * ncols.

    sharex : bool
      If True, the X axis will be shared amongst all subplots.

    sharey : bool
      If True, the Y axis will be shared amongst all subplots.

    squeeze : bool

      If True, extra dimensions are squeezed out from the returned axis object:
        - if only one subplot is constructed (nrows=ncols=1), the resulting
        single Axis object is returned as a scalar.
        - for Nx1 or 1xN subplots, the returned object is a 1-d numpy object
        array of Axis objects are returned as numpy 1-d arrays.
        - for NxM subplots with N>1 and M>1 are returned as a 2d array.

      If False, no squeezing is done: the returned axis object is always
      a 2-d array containing Axis instances, even if it ends up being 1x1.

    subplot_kw : dict
      Dict with keywords passed to the add_subplot() call used to create each
      subplots.

    ax : Matplotlib axis object, optional

    layout : tuple
      Number of rows and columns of the subplot grid.
      If not specified, calculated from naxes and layout_type

    layout_type : {'box', 'horziontal', 'vertical'}, default 'box'
      Specify how to layout the subplot grid.

    fig_kw : Other keyword arguments to be passed to the figure() call.
        Note that all keywords not recognized above will be
        automatically included here.

    Returns:

    fig, ax : tuple
      - fig is the Matplotlib Figure object
      - ax can be either a single axis object or an array of axis objects if
      more than one subplot was created.  The dimensions of the resulting array
      can be controlled with the squeeze keyword, see above.

    **Examples:**

    x = np.linspace(0, 2*np.pi, 400)
    y = np.sin(x**2)

    # Just a figure and one subplot
    f, ax = plt.subplots()
    ax.plot(x, y)
    ax.set_title('Simple plot')

    # Two subplots, unpack the output array immediately
    f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
    ax1.plot(x, y)
    ax1.set_title('Sharing Y axis')
    ax2.scatter(x, y)

    # Four polar axes
    plt.subplots(2, 2, subplot_kw=dict(polar=True))
    r   Nz5When passing multiple axes, layout keyword is ignoredznWhen passing multiple axes, sharex and sharey are ignored. These settings must be specified when creating axesr(   )
stacklevelzBThe number of passed axes must be {0}, the same as the output plotr&   zSTo output multiple subplots, the figure containing the passed axes is being cleared)r/   r0   )dtypesharexshareyF)matplotlib.pyplotpyplotZfigurer   _flattenwarningswarnUserWarningr+   r   r   r-   clearr3   npemptyobjectZadd_subplotr   copyset_visible_handle_shared_axesZreshapesqueeze)naxesr6   r7   rE   Z
subplot_kwr   r/   r0   Zfig_kwpltr   r#   r$   r%   axarrZax0ikwdsaxesr   r   r   	_subplotss   sl    L



rL   c             C   s   x|   D ]}|d q
W yjdd lm} t|  |jrH| |  t| 	 |j
rh| |d x|  D ]}|d qrW W n tk
r    Y nX |  d d S )NFr    )Zget_majorticklabelsrC   Zmatplotlib.tickertickerr   Zget_minor_locatorZNullLocatorZset_minor_locatorZAutoLocatorZget_minor_formatterZNullFormatterZset_minor_formatterZFormatStrFormatterZget_minorticklabelsr   Z	get_label)ZaxistrN   r   r   r   _remove_labels_from_axis  s    rP   c       	      C   s<  |dkr8|dkryt j|d |d ft jd}x | D ]}| ||j|jf< q6W xF| D ]>}||jd |jf srqX|st| |dkrXt	|j
 qXW W nP tk
r   x:| D ]2}| rq|st| |dkrt	|j
 qW Y nX |dkr8x>| D ]6}| rq|s*t| |dkrt	|j qW d S )Nr&   )r5   )r?   ZzerosboolZget_visibleZrowNumZcolNumr+   Zget_shared_x_axesZget_siblingsrP   Zxaxis
IndexErrorZis_last_rowZis_first_colZget_shared_y_axesZyaxis)	rH   r%   rF   r#   r$   r6   r7   r/   r   r   r   r   rD   *  s2    






rD   c             C   s6   t | st| gS t| tjtfr,|  S t| S )N)r   r?   Zarrayr   Zndarrayr   Zravel)rK   r   r   r   r:   T  s
    r:   c             C   s<   |   }t| dr || j  7 }t| dr8|| j  7 }|S )Nright_axleft_ax)	get_lineshasattrrS   rT   )r   linesr   r   r   _get_all_lines\  s    

rX   c             C   sR   t jt j  }}x8| D ]0}|jdd}tt ||}tt ||}qW ||fS )NF)Zorig)r?   infZ	get_xdataminZnanminmaxZnanmax)rW   leftr	   lr    r   r   r   	_get_xlimh  s    
r^   c             C   s   dd l m} xxt| D ]l}|d k	r4|j| |d |d k	rN|j| |d |d k	rh|j| |d |d k	r|j| |d qW | S )Nr   )Zfontsize)Zrotation)r8   r9   r:   Zsetpr
   Zget_yticklabels)rK   Z
xlabelsizeZxrotZ
ylabelsizeZyrotrG   r   r   r   r   _set_ticks_propsq  s    r_   )NN)Nr   )NFFTNNNr   )NNNN)Z
__future__r   Zmathr   r;   Znumpyr?   Zpandas.compatr   Zpandas.core.dtypes.commonr   Zpandas.core.dtypes.genericr   r   r   r   r   r3   rL   rP   rD   r:   rX   r^   r_   r   r   r   r   <module>   s&   
(
/ 
 $*	 