B
    ¤ŠãZ¤  ã               @   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 : string
        label of x-axis

    Returns
    -------
    fig : 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é   )Z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ÚfigZvalsÚaxÚxlimZspÚylimZscaleZper_varianceZcumper_variance© r   ú=lib/python3.7/site-packages/statsmodels/multivariate/plots.pyÚ
plot_scree   sT    












r   úFactor patternsc             C   s  | j \}}|dkrNg }x6t|ƒD ]*}x$t|d |ƒD ]}	| ||	g¡ q4W q W |dkrhdd„ t|ƒD ƒ}|dkr‚dd„ t|ƒD ƒ}g }
x€|D ]v}|d }|d }	tjdd}|
 |¡ | d	¡}x<t| j d ƒD ]*}tj| ||f | ||	f || d
d qÔW | | dd…|f | dd…|	f d¡ | |¡ |dk	rzd|| || f }d||	 ||	 f }| 	|¡ | 
|¡ n| 	|| ¡ | 
||	 ¡ d}t | |g¡}t | |g¡}| |ddgd¡ | ddg|d¡ | dd¡ | |¡ | |¡ | ¡  qŽW |
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>m   s    z!plot_loadings.<locals>.<listcomp>c             S   s   g | ]}d | ‘qS )zvar %dr   )r   r   r   r   r   r   o   s    r   )é   r   )Zfigsizeéo   é   )ZfontsizeZboz%s (%.1f%%)gÍÌÌÌÌÌð?zk--Z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   sN    


&




r+   )Nr   )NNNNr   )Zmatplotlib.pyplotZpyplotr   Znumpyr   r   r+   r   r   r   r   Ú<module>   s
   
G  