B
    ¤ŠãZö  ã               @   sN   d Z ddlm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 http://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
é    )ÚrangeN)Ú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lwg      à?)Zmatplotlib.patchesr   ÚnpZlinalgZeighZnormZarctanZpiZsqrtr   Zchi2ZppfZset_clip_boxZbboxZ	set_alphaZ
add_artist)ÚmeanÚcovÚaxÚlevelÚcolorr   ÚvÚwÚuZangleZell© r   ú>lib/python3.7/site-packages/statsmodels/graphics/plot_grids.pyÚ_make_ellipse   s     
r   çÍÌÌÌÌÌì?Fc          	   C   s`  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}x|td|	ƒD ]l}d}x`t|ƒD ]R}| |	d |	d |d |	d  | d ¡}| d¡}|j |¡ |j |¡ t ||g¡}|j| dd…|f j|
Ž t |¡rB|g}x>|D ]6}t|| ||dd…df |f |fd|i|—Ž qHW |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¶W q W xH|j#D ]>}| ¡ r:|j | d¡¡ | ¡ r|j | d¡¡ qW |S )aC  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 of 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 : Matplotlib figure instance, optional
        If given, this figure is simply returned.  Otherwise a new figure is
        created.

    Returns
    -------
    fig : Matplotlib figure instance
        If `fig` is None, the created figure.  Otherwise `fig` itself.

    r   Nr   c             S   s   g | ]}d | ‘qS )zvar%dr   )Ú.0Úir   r   r   ú
<listcomp>V   s    z#scatter_ellipse.<locals>.<listcomp>r   Ú.Úkg      à?)ZlsZ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Úshaper   ÚdictÚupdater   r   Zadd_subplotZFormatStrFormatterZyaxisZset_major_formatterZxaxisZarrayZplotÚTZisscalarr   Z	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   ZvarnamesZell_kwdsZ	plot_kwdsZ
add_titlesZ
keep_ticksZfigZmtickerZnvarsZ
plot_kwds_Z	ell_kwds_ZdmeanZdcovr   Zax_lastÚjr   Z	formatterÚidxr   ZdcorrZdcZxlimZylimZyrangeqZytZxtr   r   r   r   *   sp    %






*

(

.

)r   N)r   NNNFFN)Ú__doc__Zstatsmodels.compat.pythonr   Znumpyr   Zscipyr   Ú r   Ú__all__r   r   r   r   r   r   Ú<module>   s   
  