
    'YHhK                     4    d Z ddlZddlmZ 	 	 ddZ	 	 ddZy)	zcorrelation plots

Author: Josef Perktold
License: BSD-3

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

    N   )utilsc           	          |d}nd}t        j                  |      \  }}| j                  d   }	||}|d}t        |t              r|\  }
}n|rd\  }
}nd\  }
}|j                  | |dd|	d|	f|
|	      }t        j                  d|	      d
z   }t        |t              r t        |      dk(  r|j                  g        nF|D|j                  |       |j                  |dd d
z   d       |j                  |ddd   dd       t        |t              r t        |      dk(  r|j                  g        nA|?|j                  |       |j                  |dd d
z   d       |j                  |ddd       |dk(  s|j                  |       |r|j                  |d       |j!                          |j#                  dd       |j#                  ddd       	 |j%                  ddddd       |S # t&        $ r Y |S w xY w)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                 Y/var/www/html/planif/env/lib/python3.12/site-packages/statsmodels/graphics/correlation.py	plot_corrr?      s   b 
z!!"%GCKKNE~}$)U#
d	
d
d99UYuQu-Dt  ED yyE"S(H&$CK1$4
b		
h
hsmC't4
6$B$<'/6 	 	8 &$CK1$4
b		
h
hsmC't4
6Gb/6 	 	8 B;
UT-NNN+NNUUN;
Gs#!D
 J	  J	s   G0 0	G=<G=c           
      J   ||}|sdgt        |       z  }t        |       }|+t        t        j                  |t	        |      z              }	nR|dk  rd|}}	nHt        t        j
                  |            }	t        t        j                  |t	        |	      z              }t        |t	        |	      z  d      }
t        j
                  |	      dz  }t        j                  |||
z  dz   |f      }t        |       D ]Q  \  }}|j                  |	||dz         }|	|z  |dz   z
  |k  r|ng }|dz   |z  dk(  r|ng }t        |||||   |||       S |j                  dd	d
d
       |j                  g d      }|j                  |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
    r      r   g?   )figsize)r4   r5   r6   r7   r8   r   皙?g
ףp=
?g?)bottomleftr   r   )gq=
ףp?rD   g?g?r   )cax)r(   intr%   ceilfloatsqrtminr   create_mpl_fig	enumerateadd_subplotr?   subplots_adjustadd_axesr.   axesimages)dcorrstitlesncolsr7   r4   r5   r:   r   n_plotsnrowsaspectvsizeicr8   _xnames_ynamesrG   s                     r>   plot_corr_gridr_   }   s   x ~c&k!&kGBGGGeEl234 Q;g5E()E%, 678E u%s+FGGENQE


sUV^a-?,G
HC&! 91__UE1Q3/!EMQqS1E9&rqSEMQ.&B!GG6!9%"4	99 sScB
,,.
/CLL!##A&CL0J    )NNNFNRdYlBu_r)NNFNNNra   )__doc__numpyr%   r   r   r?   r_    r`   r>   <module>re      s3      FK&k\ MQ/9`r`   