U
    ÃmœdK  ã                   @   s0   d Z ddlZddlmZ ddd„Zdd	d
„ZdS )z correlation 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ƒr¼t	|ƒ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      à?éÿÿÿÿ)ÚminorZ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_paramsÚgridÚAttributeError)ZdcorrÚxnamesÚynamesÚtitleÚ	normcolorÚaxr   Zcreate_colorbarÚfigZnvarsr   r   ZaximZlabelPos© r(   úY/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/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ÍÌÌÌÌÌì?)Ú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    <  ÿr9   )NNNFNr   )NNFNNNr   )Ú__doc__Únumpyr   r   r   r*   r9   r(   r(   r(   r)   Ú<module>   s   	    ÿ
n      ÿ