U
    KvfK                     @   s0   d Z ddlZddlmZ dddZdd	d
ZdS )zcorrelation plots

Author: Josef Perktold
License: BSD-3

example for usage with different options in
statsmodels/sandbox/examples/thirdparty/ex_ratereturn.py

    N   )utilsFRdYlBu_rc                 C   s  |dkrd}nd}t |\}}| jd }	|dkr6|}|dkrBd}t|trV|\}
}n|rdd\}
}nd\}
}|j| |dd|	d|	f|
|d	}td|	d
 }t|trt	|dkr|
g  nH|dk	r|
| |j
|dd d
 dd |j|ddd ddd t|tr*t	|dkr*|g  n@|dk	rj|| |j|dd d
 dd |j|dddd |dks~|| |r|j|dd |  |jddd |jdddd z|jdddddd W n tk
r   Y nX |S )aJ  Plot correlation of many variables in a tight color grid.

    Parameters
    ----------
    dcorr : ndarray
        Correlation matrix, square 2-D array.
    xnames : list[str], optional
        Labels for the horizontal axis.  If not given (None), then the
        matplotlib defaults (integers) are used.  If it is an empty list, [],
        then no ticks and labels are added.
    ynames : list[str], optional
        Labels for the vertical axis.  Works the same way as `xnames`.
        If not given, the same names as for `xnames` are re-used.
    title : str, optional
        The figure title. If None, the default ('Correlation Matrix') is used.
        If ``title=''``, then no title is added.
    normcolor : bool or tuple of scalars, optional
        If False (default), then the color coding range corresponds to the
        range of `dcorr`.  If True, then the color range is normalized to
        (-1, 1).  If this is a tuple of two numbers, then they define the range
        for the color bar.
    ax : AxesSubplot, optional
        If `ax` is None, then a figure is created. If an axis instance is
        given, then only the main plot but not the colorbar is created.
    cmap : str or Matplotlib Colormap instance, optional
        The colormap for the plot.  Can be any valid Matplotlib Colormap
        instance or name.

    Returns
    -------
    Figure
        If `ax` is None, the created figure.  Otherwise the figure to which
        `ax` is connected.

    Examples
    --------
    >>> import numpy as np
    >>> import matplotlib.pyplot as plt
    >>> import statsmodels.graphics.api as smg

    >>> hie_data = sm.datasets.randhie.load_pandas()
    >>> corr_matrix = np.corrcoef(hie_data.data.T)
    >>> smg.plot_corr(corr_matrix, xnames=hie_data.names)
    >>> plt.show()

    .. plot:: plots/graphics_correlation_plot_corr.py
    NTFr   zCorrelation Matrix)g      g      ?)NNZnearest)cmapinterpolationZextentvminvmaxg      ?)minorsmallright)fontsizehorizontalalignment-   )r   Zrotationr    )Zuse_gridspecr
   )whichlengthout)	directiontopr   -wr   )r   Z	linestylecolorZlw)r   Zcreate_mpl_axshape
isinstancetupleZimshownpZarangelistlenZ
set_yticksZset_yticklabelsZ
set_xticksZset_xticklabels	set_titlecolorbarZtight_layoutZtick_paramsZgridAttributeError)Zdcorrxnamesynamestitle	normcoloraxr   Zcreate_colorbarfigZnvarsr   r   ZaximZlabelPos r(   D/tmp/pip-unpacked-wheel-2v6byqio/statsmodels/graphics/correlation.py	plot_corr   s`    1





  






r*   c              
   C   sn  |dkr|}|sdgt |  }t | }|dk	rFtt|t| }	n8|dk rZd| }	}n$tt|}	tt|t|	 }t|t|	 d}
t|	d }tj|||
 d |fd}t	| D ]j\}}|
|	||d }|	| |d  |k r|ng }|d | dkr|ng }t||||| |||d q|jd	d
ddd |dd	ddg}|j|jd jd |d |S )a	  
    Create a grid of correlation plots.

    The individual correlation plots are assumed to all have the same
    variables, axis labels can be specified only once.

    Parameters
    ----------
    dcorrs : list or iterable of ndarrays
        List of correlation matrices.
    titles : list[str], optional
        List of titles for the subplots.  By default no title are shown.
    ncols : int, optional
        Number of columns in the subplot grid.  If not given, the number of
        columns is determined automatically.
    normcolor : bool or tuple, optional
        If False (default), then the color coding range corresponds to the
        range of `dcorr`.  If True, then the color range is normalized to
        (-1, 1).  If this is a tuple of two numbers, then they define the range
        for the color bar.
    xnames : list[str], optional
        Labels for the horizontal axis.  If not given (None), then the
        matplotlib defaults (integers) are used.  If it is an empty list, [],
        then no ticks and labels are added.
    ynames : list[str], optional
        Labels for the vertical axis.  Works the same way as `xnames`.
        If not given, the same names as for `xnames` are re-used.
    fig : Figure, optional
        If given, this figure is simply returned.  Otherwise a new figure is
        created.
    cmap : str or Matplotlib Colormap instance, optional
        The colormap for the plot.  Can be any valid Matplotlib Colormap
        instance or name.

    Returns
    -------
    Figure
        If `ax` is None, the created figure.  Otherwise the figure to which
        `ax` is connected.

    Examples
    --------
    >>> import numpy as np
    >>> import matplotlib.pyplot as plt
    >>> import statsmodels.api as sm

    In this example we just reuse the same correlation matrix several times.
    Of course in reality one would show a different correlation (measuring a
    another type of correlation, for example Pearson (linear) and Spearman,
    Kendall (nonlinear) correlations) for the same variables.

    >>> hie_data = sm.datasets.randhie.load_pandas()
    >>> corr_matrix = np.corrcoef(hie_data.data.T)
    >>> sm.graphics.plot_corr_grid([corr_matrix] * 8, xnames=hie_data.names)
    >>> plt.show()

    .. plot:: plots/graphics_correlation_plot_corr_grid.py
    Nr      r   g?   )Zfigsize)r"   r#   r$   r%   r&   r   g?g
ףp=
?g?)Zbottomleftr   r   gq=
ףp?g?g?r   )cax)r   intr   ceilfloatsqrtminr   Zcreate_mpl_fig	enumerateZadd_subplotr*   Zsubplots_adjustZadd_axesr    ZaxesZimages)ZdcorrstitlesZncolsr%   r"   r#   r'   r   Zn_plotsZnrowsZaspectZvsizeicr&   Z_xnamesZ_ynamesr.   r(   r(   r)   plot_corr_grid}   s6    <  r8   )NNNFNr   )NNFNNNr   )__doc__Znumpyr   r   r   r*   r8   r(   r(   r(   r)   <module>   s   	    
n      