U
    Kvf©  ã                   @   sB   d Z ddlZddlmZ ddlmZ dgZddd	„Zddd„Z	dS )a¯  create scatterplot with confidence ellipsis

Author: Josef Perktold
License: BSD-3

TODO: update script to use sharex, sharey, and visible=False
    see https://www.scipy.org/Cookbook/Matplotlib/Multiple_Subplots_with_One_Axis_Label
    for sharex I need to have the ax of the last_row when editing the earlier
    rows. Or you axes_grid1, imagegrid
    http://matplotlib.sourceforge.net/mpl_toolkits/axes_grid/users/overview.html
é    N)Ústatsé   )ÚutilsÚscatter_ellipseçffffffî?c              	   C   sÂ   ddl m} tj |¡\}}|d tj |d ¡ }t |d |d  ¡}	d|	 tj }	dt |t	j
 |d¡ ¡ }|| dd… |d |d d|	 d|dd	}
|
 |j¡ |
 d
¡ | |
¡ dS )z%Support function for scatter_ellipse.r   )ÚEllipser   é´   é   NÚnoneg      ø?)Z	facecolorZ	edgecolorZlwç      à?)Zmatplotlib.patchesr   ÚnpZlinalgZeighZnormZarctanÚpiÚsqrtr   Zchi2ZppfZset_clip_boxZbboxZ	set_alphaZ
add_artist)ÚmeanÚcovÚaxÚlevelÚcolorr   ÚvÚwÚuZangleZell© r   úC/tmp/pip-unpacked-wheel-2v6byqio/statsmodels/graphics/plot_grids.pyÚ_make_ellipse   s     ý
r   çÍÌÌÌÌÌì?Fc              	   C   sL  t  |¡}ddlm} t | ¡} | jd }	|dkrDdd„ t|	ƒD ƒ}tdddd	d
}
|rb|
 	|¡ tdd}|rz| 	|¡ |  
d¡}tj| dd}td|	ƒD ]b}d}t|ƒD ]N}| |	d |	d |d |	d  | d ¡}| d¡}|j |¡ |j |¡ t ||g¡}|j| dd…|f j|
Ž t |¡r:|g}|D ]6}t|| ||dd…df |f |fd|i|—Ž q>|r–| d|| || f ¡ | ¡ sÆ|s²| g ¡ n|j | d¡¡ n| || ¡ | ¡ rî| || ¡ n$|s | g ¡ n|j | d¡¡ tj| dd}||dd…df |f }|  ¡ }| !¡ }|d d|d |d    }|d dk s’|d dk r°|| d |kr°|d d|d |d    }n|d d|d |d    }|d d|d |d    }| "||d|d  ¡ q®qœ|j#D ]>}| ¡ r(|j | d¡¡ | ¡ r|j | d¡¡ q|S )aÈ  Create a grid of scatter plots with confidence ellipses.

    ell_kwds, plot_kdes not used yet

    looks ok with 5 or 6 variables, too crowded with 8, too empty with 1

    Parameters
    ----------
    data : array_like
        Input data.
    level : scalar, optional
        Default is 0.9.
    varnames : list[str], optional
        Variable names.  Used for y-axis labels, and if `add_titles` is True
        also for titles.  If not given, integers 1..data.shape[1] are used.
    ell_kwds : dict, optional
        UNUSED
    plot_kwds : dict, optional
        UNUSED
    add_titles : bool, optional
        Whether or not to add titles to each subplot.  Default is False.
        Titles are constructed from `varnames`.
    keep_ticks : bool, optional
        If False (default), remove all axis ticks.
    fig : Figure, optional
        If given, this figure is simply returned.  Otherwise a new figure is
        created.

    Returns
    -------
    Figure
        If `fig` is None, the created figure.  Otherwise `fig` itself.

    Examples
    --------
    >>> import statsmodels.api as sm
    >>> import matplotlib.pyplot as plt
    >>> import numpy as np

    >>> from statsmodels.graphics.plot_grids import scatter_ellipse
    >>> data = sm.datasets.statecrime.load_pandas().data
    >>> fig = plt.figure(figsize=(8,8))
    >>> scatter_ellipse(data, varnames=data.columns, fig=fig)
    >>> plt.show()

    .. plot:: plots/graphics_plot_grids_scatter_ellipse.py
    r   Nr   c                 S   s   g | ]}d | ‘qS )zvar%dr   )Ú.0Úir   r   r   Ú
<listcomp>a   s     z#scatter_ellipse.<locals>.<listcomp>r
   Ú.Úkr   )ZlsÚmarkerr   Úalpha)r   )Zrowvarz% 3.1fr   z%s-%sé   gš™™™™™Ù?)r   r   g      Ð¿g      Ð?gš™™™™™¹?gš™™™™™É?z$\rho=%0.2f$)$r   Zcreate_mpl_figZmatplotlib.tickerZtickerr   Z
asanyarrayÚshapeÚrangeÚdictÚupdater   r   Zadd_subplotZFormatStrFormatterZyaxisZset_major_formatterZxaxisÚarrayZplotÚTZisscalarr   Ú	set_titleZis_first_colZ
set_yticksZset_major_locatorZMaxNLocatorZ
set_ylabelZis_last_rowZ
set_xlabelZ
set_xticksZcorrcoefZget_xlimZget_ylimÚtextZaxes)Údatar   ÚvarnamesZell_kwdsZ	plot_kwdsZ
add_titlesZ
keep_ticksZfigZmtickerZnvarsZ
plot_kwds_Z	ell_kwds_ZdmeanZdcovr   Zax_lastÚjr   Ú	formatterÚidxr!   ZdcorrÚdcZxlimZylimZyrangeqZytZxtr   r   r   r   (   sr    2






*
(ÿ


.


)r   N)r   NNNFFN)
Ú__doc__Znumpyr   Zscipyr   Ú r   Ú__all__r   r   r   r   r   r   Ú<module>   s   
       þ