o
    þ0Gf  ã                   @   s2   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u 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ÚfigZvalsÚaxÚxlimÚspÚylimZscaleZper_varianceZcumper_variance© r   ú>lib/python3.10/site-packages/statsmodels/multivariate/plots.pyÚ
plot_scree   sT   












r   úFactor patternsc                 C   sø  | j \}}|du r#g }t|ƒD ]}t|d |ƒD ]	}	| ||	g¡ qq|du r0dd„ t|ƒD ƒ}|du r=dd„ t|ƒD ƒ}g }
|D ]¸}|d }|d }	tjdd}|
 |¡ | d	¡}t| j d ƒD ]}tj| ||f | ||	f || d
d qb| | dd…|f | dd…|	f d¡ | |¡ |dur³d 	|| || ¡}d 	||	 ||	 ¡}| 
|¡ | |¡ n| 
|| ¡ | ||	 ¡ d}t | |g¡}t | |g¡}| |ddgd¡ | ddg|d¡ | dd¡ | |¡ | |¡ | ¡  qA|
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   ó   g | ]}d | ‘qS )z	factor %dr   ©Ú.0Úir   r   r   Ú
<listcomp>l   ó    z!plot_loadings.<locals>.<listcomp>c                 S   r   )zvar %dr   r   r   r   r   r"   n   r#   r   )é   r$   )Zfigsizeéo   é   )ZfontsizeZboz{} ({:.1f}%)gÍÌÌÌÌÌð?zk--ZequalZdatalim)ÚshapeÚrangeÚappendr   r	   r   Útextr   r   Úformatr   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   
ÿ

ÿ&




r1   )Nr   )NNNNr   )Zmatplotlib.pyplotZpyplotr   Znumpyr   r   r1   r   r   r   r   Ú<module>   s    
Gþ