U
    Kvf  ã                   @   s,   d dl mZ d dlZddd„Zd	dd„ZdS )
é    NÚfactorc                 C   s@  t  ¡ }|dkrt| ƒn|}| }|d|… }| d¡}| t |¡|d|… d¡ |jdd t | 	¡ ¡}|d |d  }|dt | |g¡ 7 }| 
t |¡¡ | |¡ t | ¡ ¡}	d}
|	d |	d  }|	|
t | |g¡ 7 }	| |	¡ | d	¡ | d
¡ | |¡ || }t |¡}| d¡}| t |¡|d|… d¡ | t |¡|d|… d¡ |jdd t | 	¡ ¡}|d |d  }|dt | |g¡ 7 }| 
t |¡¡ | |¡ t | ¡ ¡}	d}
|	d |	d  }|	|
t | |g¡ 7 }	| |	¡ | d¡ | d¡ | |¡ |jddgdd | ¡  |S )a
  
    Plot of the ordered eigenvalues and variance explained for the loadings

    Parameters
    ----------
    eigenvals : array_like
        The eigenvalues
    total_var : float
        the total variance (for plotting percent variance explained)
    ncomp : int, optional
        Number of factors to include in the plot.  If None, will
        included the same as the number of maximum possible loadings
    x_label : str
        label of x-axis

    Returns
    -------
    Figure
        Handle to the figure.
    Néy   zb-oT)Ztighté   r   g{®Gáz”?z
Scree PlotZ
Eigenvalueéz   zg--ozVariance ExplainedZ
ProportionZ
Cumulativeé   )Úloc)ÚpltÚfigureÚlenÚadd_subplotÚplotÚnpZarangeZ	autoscaleÚarrayZget_xlimZ
set_xticksÚset_xlimZget_ylimÚset_ylimÚ	set_titleÚ
set_ylabelÚ
set_xlabelZcumsumZlegendÚtight_layout)Z	eigenvalsZ	total_varZncompZx_labelÚfigÚvalsÚaxÚxlimÚspÚylimZscaleZper_varianceZcumper_variance© r   úB/tmp/pip-unpacked-wheel-2v6byqio/statsmodels/multivariate/plots.pyÚ
plot_scree   sT    












r   úFactor patternsc                 C   sü  | j \}}|dkrFg }t|ƒD ]&}t|d |ƒD ]}	| ||	g¡ q0q|dkr`dd„ t|ƒD ƒ}|dkrzdd„ t|ƒD ƒ}g }
|D ]r}|d }|d }	tjdd}|
 |¡ | d	¡}t| j d ƒD ]*}tj| ||f | ||	f || d
d qÆ| | dd…|f | dd…|	f d¡ | |¡ |dk	rjd|| || f }d||	 ||	 f }| 	|¡ | 
|¡ n| 	|| ¡ | 
||	 ¡ d}t | |g¡}t | |g¡}| |ddgd¡ | ddg|d¡ | dd¡ | |¡ | |¡ | ¡  q‚|
S )a¬  
    Plot factor loadings in 2-d plots

    Parameters
    ----------
    loadings : array like
        Each column is a component (or factor)
    col_names : a list of strings
        column names of `loadings`
    row_names : a list of strings
        row names of `loadings`
    loading_pairs : None or a list of tuples
        Specify plots. Each tuple (i, j) represent one figure, i and j is
        the loading number for x-axis and y-axis, respectively. If `None`,
        all combinations of the loadings will be plotted.
    percent_variance : array_like
        The percent variance explained by each factor.

    Returns
    -------
    figs : a list of figure handles
    Nr   c                 S   s   g | ]}d | ‘qS )z	factor %dr   ©Ú.0Úir   r   r   Ú
<listcomp>l   s     z!plot_loadings.<locals>.<listcomp>c                 S   s   g | ]}d | ‘qS )zvar %dr   r   r   r   r   r"   n   s     r   )é   r#   )Zfigsizeéo   é   )ZfontsizeZboz%s (%.1f%%)gÍÌÌÌÌÌð?zk--ÚequalZdatalim)ÚshapeÚrangeÚappendr   r	   r   Útextr   r   r   r   r   r   Z
set_aspectr   r   r   )ZloadingsZ	col_namesZ	row_namesZloading_pairsZpercent_varianceÚtitleZk_varZn_factorr!   ÚjZfigsÚitemr   r   ÚkZx_strZy_strÚvr   r   r   r   r   Úplot_loadingsL   sR    



 ÿ&





r0   )Nr   )NNNNr   )Zmatplotlib.pyplotZpyplotr   Znumpyr   r   r0   r   r   r   r   Ú<module>   s   
G     þ