
    M/PhK                     6    d Z ddlZddlmZ 	 	 d	dZ	 	 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           	         |d}nd}t          j        |          \  }}| j        d         }	||}|d}t          |t                    r|\  }
}n|rd\  }
}nd\  }
}|                    | |dd|	d|	f|
|	          }t          j        d|	          d
z   }t          |t                    r)t          |          dk    r|
                    g            nZ|X|
                    |           |
                    |dd         d
z   d           |                    |ddd         dd           t          |t                    r)t          |          dk    r|                    g            nR|P|                    |           |                    |dd         d
z   d           |                    |ddd           |dk    s|                    |           |r|                    |d           |                                 |                    dd           |                    ddd           	 |                    ddddd           n# t&          $ r Y nw xY w|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      ?)NNnearest)cmapinterpolationextentvminvmaxg      ?)minorsmallright)fontsizehorizontalalignment-   )r   rotationr    )use_gridspecr   )whichlengthout)	directiontopr   -wr   )r   	linestylecolorlw)r   create_mpl_axshape
isinstancetupleimshownparangelistlen
set_yticksset_yticklabels
set_xticksset_xticklabels	set_titlecolorbartight_layouttick_paramsgridAttributeError)dcorrxnamesynamestitle	normcoloraxr   create_colorbarfignvarsr   r   aximlabelPoss                 `/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/graphics/correlation.py	plot_corrr@      s   b 
z!"%%GCKNE~}$)U##  
dd	  
dd
d99UYuQu-Dt  E ED yE""S(H&$ 8CKK1$4$4
b		
h
hssmC't444
6$$B$<'/6 	 	8 	8 	8 &$ 8CKK1$4$4
b		
h
hssmC't444
6Gb/6 	 	8 	8 	8 B;;
U .T---NNN+++NNUUN;;;
Gs#!DDDD    Js   3I 
IIc           
         ||}|sdgt          |           z  }t          |           }|2t          t          j        |t	          |          z                      }	n]|dk     rd|}}	nRt          t          j        |                    }	t          t          j        |t	          |	          z                      }t          |t	          |	          z  d          }
t          j        |	          dz  }t          j        |||
z  dz   |f          }t          |           D ]^\  }}|
                    |	||dz             }|	|z  |dz   z
  |k     r|ng }|dz   |z  dk    r|ng }t          |||||         |||           _|                    d	d
dd           |                    g d          }|                    |j        d         j        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?   )figsize)r5   r6   r7   r8   r9   r   皙?g
ףp=
?g?)bottomleftr   r   )gq=
ףp?rE   g?g?r   )cax)r)   intr&   ceilfloatsqrtminr   create_mpl_fig	enumerateadd_subplotr@   subplots_adjustadd_axesr/   axesimages)dcorrstitlesncolsr8   r5   r6   r;   r   n_plotsnrowsaspectvsizeicr9   _xnames_ynamesrH   s                     r?   plot_corr_gridr`   }   s   x ~ "c&kk!&kkGBGGeEll23344 Q;;g5EE(())E%,, 67788E u%s++FGENNQE

sUV^a-?,G
H
H
HC&!! 9 91__UE1Q3//!EMQqS1E99&&rqSEMQ..&&B!GG6!9%"4	9 	9 	9 	9 	9 sScBBB
,,...
/
/CLL!#A&CL000J    )NNNFNr   )NNFNNNr   )__doc__numpyr&   r   r   r@   r`    ra   r?   <module>re      s               FK&k k k k\ MQ/9` ` ` ` ` `ra   