B
    Z                 @   sX   d Z ddlmZmZmZ ddgZdd ZdddZdd	dZd
d Z	dd Z
dd ZdS )z.Helper functions for graphics with Matplotlib.    )lrangerangelongcreate_mpl_axcreate_mpl_figc              C   s*   yddl m}  W n   tdY nX | S )z6This function is not needed outside this utils module.r   NzMatplotlib is not found.)Zmatplotlib.pyplotZpyplotImportError)plt r	   9lib/python3.7/site-packages/statsmodels/graphics/utils.py_import_mpl   s
    r   Nc             C   s0   | dkr"t  }| }|d} n| j}|| fS )a  Helper function for when a single plot axis is needed.

    Parameters
    ----------
    ax : Matplotlib AxesSubplot instance, optional
        If given, this subplot is used to plot in instead of a new figure being
        created.

    Returns
    -------
    fig : Matplotlib figure instance
        If `ax` is None, the created figure.  Otherwise the figure to which
        `ax` is connected.
    ax : Matplotlib AxesSubplot instance
        The created axis if `ax` is None, otherwise the axis that was passed
        in.

    Notes
    -----
    This function imports `matplotlib.pyplot`, which should only be done to
    create (a) figure(s) with ``plt.figure``.  All other functionality exposed
    by the pyplot module can and should be imported directly from its
    Matplotlib module.

    See Also
    --------
    create_mpl_fig

    Examples
    --------
    A plotting function has a keyword ``ax=None``.  Then calls:

    >>> from statsmodels.graphics import utils
    >>> fig, ax = utils.create_mpl_ax(ax)

    No   )r   figureZadd_subplot)axr   figr	   r	   r
   r      s    %c             C   s   | dkrt  }|j|d} | S )a  Helper function for when multiple plot axes are needed.

    Those axes should be created in the functions they are used in, with
    ``fig.add_subplot()``.

    Parameters
    ----------
    fig : Matplotlib figure instance, optional
        If given, this figure is simply returned.  Otherwise a new figure is
        created.

    Returns
    -------
    fig : Matplotlib figure instance
        If `fig` is None, the created figure.  Otherwise the input `fig` is
        returned.

    See Also
    --------
    create_mpl_ax

    N)figsize)r   r   )r   r   r   r	   r	   r
   r   @   s    c             C   s   | dkrt |jjd } t| ttfr6|j|  }| }nXt| ttfr~g }g }x@| D ]&}t	||\}}|
| |
| qRW n| }|j| }||fS )zr
    Give a name or an integer and return the name and integer location of the
    column in a design matrix.
    N   )r   Zexogshape
isinstanceintr   Z
exog_namestuplelistmaybe_name_or_idxappendindex)idxZmodelZ	exog_nameZexog_idxitemZexog_name_itemZexog_idx_itemr	   r	   r
   r   ^   s    


r   c             C   sx   t | dd}|st | dd}|slt | ddg}t|dkr>dntjd }dd t|D }|dkrt|d }n| }|S )	z
    Input can be an array or pandas-like. Will handle 1d array-like but not
    2d. Returns a str for 1d data or a list of strings for 2d data.
    nameNcolumnsr   r   c             S   s   g | ]}d qS )zX%dr	   ).0namesr	   r	   r
   
<listcomp>   s    z"get_data_names.<locals>.<listcomp>r   )getattrlenZseriesr   r   tolist)Zseries_or_dataframer   r   Znvarsr	   r	   r
   get_data_namesw   s    
r$   c             K   sF   x@| D ]8}|| }|| }	|| }
|j ||	f|
d|d| qW |S )z\
    Annotate Axes with labels, points, offset_points according to the
    given index.
    zoffset points)ZxytextZ
textcoordssize)Zannotate)r   labelsZpointsZoffset_pointsr%   r   kwargsiZlabelZpointoffsetr	   r	   r
   annotate_axes   s    
r*   )N)NN)__doc__Zstatsmodels.compat.pythonr   r   r   __all__r   r   r   r   r$   r*   r	   r	   r	   r
   <module>   s   

/
