U
    Kvf                     @   sP   d Z ddl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create_mpl_axcreate_mpl_figc                  C   s*   z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   >/tmp/pip-unpacked-wheel-2v6byqio/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 : AxesSubplot, optional
        If given, this subplot is used to plot in instead of a new figure being
        created.

    Returns
    -------
    fig : Figure
        If `ax` is None, the created figure.  Otherwise the figure to which
        `ax` is connected.
    ax : AxesSubplot
        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 : Figure, optional
        If given, this figure is simply returned.  Otherwise a new figure is
        created.

    Returns
    -------
    Figure
        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r2|j|  }| }nTt| ttfrvg }g }| D ]&}t||\}}|	| |	| qLn| }|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intZ
exog_namestuplelistmaybe_name_or_idxappendindex)idx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| 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_r   r   r   
<listcomp>   s     z"get_data_names.<locals>.<listcomp>r   )getattrlenr   rangetolist)Zseries_or_dataframenamesr   Znvarsr   r   r   get_data_namesu   s    
r%   c                 K   sB   | D ]8}|| }|| }	|| }
|j ||	f|
d|d| q|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ilabelZpointoffsetr   r   r   annotate_axes   s    r,   )N)NN)
__doc__Zstatsmodels.compat.pythonr   __all__r	   r   r   r   r%   r,   r   r   r   r   <module>   s   

.
