ó
áp7]c           @   s_   d  Z  d d l Z d d l m Z d d d e d d d „ Z d d e d d d d d „ Z d S(   s    correlation plots

Author: Josef Perktold
License: BSD-3

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

iÿÿÿÿNi   (   t   utilst   RdYlBu_rc         C   s•  | d k r t } n t } t j | ƒ \ } } |  j d }	 | d k rR | } n  | d k rg d } n  t | t ƒ r… | \ }
 } n! | rš d" \ }
 } n d# \ }
 } | j |  d | d d d d |	 d |	 f d	 |
 d
 | ƒ} t	 j
 d |	 ƒ d } | d k	 rR| j | ƒ | j | d  d d t ƒ| j | d d d … d d d d ƒn | g  k rn| j g  ƒ n  | d k	 rÄ| j | ƒ | j | d  d d t ƒ| j | d d d d d d ƒn | g  k rà| j g  ƒ n  | d k sü| j | ƒ n  | r| j | d t ƒn  | j ƒ  | j d d d d ƒ | j d d d t d t ƒ y) | j t d d d d d d d  d! ƒWn t k
 rn X| S($   s|  Plot correlation of many variables in a tight color grid.

    Parameters
    ----------
    dcorr : ndarray
        Correlation matrix, square 2-D array.
    xnames : list of 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 of 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 : Matplotlib AxesSubplot instance, 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
    -------
    fig : Matplotlib figure instance
        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
    i    s   Correlation Matrixg      ð¿g      ð?t   cmapt   interpolationt   nearestt   extentt   vmint   vmaxg      à?iÿÿÿÿt   minorNt   fontsizet   smallt   horizontalalignmentt   rightt   rotationi-   t    t   use_gridspect   whicht   lengtht	   directiont   outt   topt	   linestylet   -t   colort   wt   lwi   (   g      ð¿g      ð?(   NN(   t   Nonet   Truet   FalseR    t   create_mpl_axt   shapet
   isinstancet   tuplet   imshowt   npt   aranget
   set_ytickst   set_yticklabelst
   set_xtickst   set_xticklabelst	   set_titlet   colorbart   tight_layoutt   tick_paramst   gridt   AttributeError(   t   dcorrt   xnamest   ynamest   titlet	   normcolort   axR   t   create_colorbart   figt   nvarsR   R   t   aximt   labelPos(    (    s?   lib/python2.7/site-packages/statsmodels/graphics/correlation.pyt	   plot_corr   sV    1			!


)c         C   s  | d k r | } n  | s1 d g t |  ƒ } n  t |  ƒ } | d k	 rk t t j | t | ƒ ƒ ƒ }	 nP | d k  r‡ d | }	 } n4 t t j | ƒ ƒ }	 t t j | t |	 ƒ ƒ ƒ } t | t |	 ƒ d ƒ }
 t j |	 ƒ d } t j	 | d | |
 d | f ƒ} x© t
 |  ƒ D]› \ } } | j |	 | | d ƒ } |	 | | d | k  rZ| n g  } | d | d k rz| n g  } t | d | d | d	 | | d
 | d | d | ƒqW| j d d d d d d d d ƒ | j d d d d g ƒ } | j | j d j d d | ƒ| S(   sG	  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 of 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 of 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 of 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 : Matplotlib figure instance, 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
    -------
    fig : Matplotlib figure instance
        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
    R   i   i   gÍÌÌÌÌÌü?i   t   figsizeR/   R0   R1   R2   R3   R   t   bottomgš™™™™™¹?t   leftg
×£p=
·?R   gÍÌÌÌÌÌì?R   gq=
×£pí?gš™™™™™™?gš™™™™™é?i    t   caxN(   R   t   lent   intR"   t   ceilt   floatt   sqrtt   minR    t   create_mpl_figt	   enumeratet   add_subplotR9   t   subplots_adjustt   add_axesR)   t   axest   images(   t   dcorrst   titlest   ncolsR2   R/   R0   R5   R   t   n_plotst   nrowst   aspectt   vsizet   it   cR3   t   _xnamest   _ynamesR=   (    (    s?   lib/python2.7/site-packages/statsmodels/graphics/correlation.pyt   plot_corr_grid|   s0    ;	"#$ "!(	   t   __doc__t   numpyR"   R   R    R   R   R9   RV   (    (    (    s?   lib/python2.7/site-packages/statsmodels/graphics/correlation.pyt   <module>	   s   l