U
    Y»{jX7  ã                   @   sš   d dl mZ d dlmZmZ d dlmZ d dlmZ e 	d¡Z
e 	d¡Ze 	d¡Ze 	d¡Zd	Zd
Zdd„ Zddddedddfdd„ZG dd„ deƒZdS )é    )Úabsolute_import)Ú
exceptionsÚoptional_imports)Úutils)Ú
graph_objsÚnumpyZpandasÚscipyzscipy.statszprobability densityZprobabilityc                 C   sh   t f}tr|tjf7 }tr*|tjjjf7 }t| d |ƒsBt 	d¡‚d}||krXt 	d¡‚t
sdtdƒ‚dS )zÃ
    Distplot-specific validations

    :raises: (PlotlyError) If hist_data is not a list of lists
    :raises: (PlotlyError) If curve_type is not valid (i.e. not 'kde' or
        'normal').
    r   z¼Oops, this function was written to handle multiple datasets, if you want to plot just one, make sure your hist_data variable is still a list of lists, i.e. x = [1, 2, 3] -> x = [[1, 2, 3]])ÚkdeÚnormalz/curve_type must be defined as 'kde' or 'normal'z,FigureFactory.create_distplot requires scipyN)ÚlistÚnpZndarrayÚpdÚcoreZseriesZSeriesÚ
isinstancer   ZPlotlyErrorr   ÚImportError)Ú	hist_dataÚ
curve_typeZhist_data_typesZ
curve_opts© r   ú^/var/www/html/bwave/python/venv/lib/python3.8/site-packages/plotly/figure_factory/_distplot.pyÚvalidate_distplot   s     ÿ	ÿr   ç      ð?r	   NTc
                 C   sŒ  |dkrg }|dkrg }t | |ƒ t | |¡ t|ttfƒrJ|gt| ƒ }g }
|rxt| ||||||||ƒ	 ¡ }|
 	|¡ |rÈ|dkr¢t| ||||||||ƒ	 
¡ }nt| ||||||||ƒ	 ¡ }|
 	|¡ |	�r>t| ||||||||ƒ	 ¡ }|
 	|¡ tjddtdd�tddgd	d
d�tddgddd�tddgddd
d�d�}n6tjddtdd�tddgd	d
d�tddgddd�d�}t|
g ƒ}
tj|
|d�S )aB  
    Function that creates a distplot similar to seaborn.distplot;
    **this function is deprecated**, use instead :mod:`plotly.express`
    functions, for example

    >>> import plotly.express as px
    >>> tips = px.data.tips()
    >>> fig = px.histogram(tips, x="total_bill", y="tip", color="sex", marginal="rug",
    ...                    hover_data=tips.columns)
    >>> fig.show()


    The distplot can be composed of all or any combination of the following
    3 components: (1) histogram, (2) curve: (a) kernel density estimation
    or (b) normal curve, and (3) rug plot. Additionally, multiple distplots
    (from multiple datasets) can be created in the same plot.

    :param (list[list]) hist_data: Use list of lists to plot multiple data
        sets on the same plot.
    :param (list[str]) group_labels: Names for each data set.
    :param (list[float]|float) bin_size: Size of histogram bins.
        Default = 1.
    :param (str) curve_type: 'kde' or 'normal'. Default = 'kde'
    :param (str) histnorm: 'probability density' or 'probability'
        Default = 'probability density'
    :param (bool) show_hist: Add histogram to distplot? Default = True
    :param (bool) show_curve: Add curve to distplot? Default = True
    :param (bool) show_rug: Add rug to distplot? Default = True
    :param (list[str]) colors: Colors for traces.
    :param (list[list]) rug_text: Hovertext values for rug_plot,
    :return (dict): Representation of a distplot figure.

    Example 1: Simple distplot of 1 data set

    >>> from plotly.figure_factory import create_distplot

    >>> hist_data = [[1.1, 1.1, 2.5, 3.0, 3.5,
    ...               3.5, 4.1, 4.4, 4.5, 4.5,
    ...               5.0, 5.0, 5.2, 5.5, 5.5,
    ...               5.5, 5.5, 5.5, 6.1, 7.0]]
    >>> group_labels = ['distplot example']
    >>> fig = create_distplot(hist_data, group_labels)
    >>> fig.show()


    Example 2: Two data sets and added rug text

    >>> from plotly.figure_factory import create_distplot
    >>> # Add histogram data
    >>> hist1_x = [0.8, 1.2, 0.2, 0.6, 1.6,
    ...            -0.9, -0.07, 1.95, 0.9, -0.2,
    ...            -0.5, 0.3, 0.4, -0.37, 0.6]
    >>> hist2_x = [0.8, 1.5, 1.5, 0.6, 0.59,
    ...            1.0, 0.8, 1.7, 0.5, 0.8,
    ...            -0.3, 1.2, 0.56, 0.3, 2.2]

    >>> # Group data together
    >>> hist_data = [hist1_x, hist2_x]

    >>> group_labels = ['2012', '2013']

    >>> # Add text
    >>> rug_text_1 = ['a1', 'b1', 'c1', 'd1', 'e1',
    ...       'f1', 'g1', 'h1', 'i1', 'j1',
    ...       'k1', 'l1', 'm1', 'n1', 'o1']

    >>> rug_text_2 = ['a2', 'b2', 'c2', 'd2', 'e2',
    ...       'f2', 'g2', 'h2', 'i2', 'j2',
    ...       'k2', 'l2', 'm2', 'n2', 'o2']

    >>> # Group text together
    >>> rug_text_all = [rug_text_1, rug_text_2]

    >>> # Create distplot
    >>> fig = create_distplot(
    ...     hist_data, group_labels, rug_text=rug_text_all, bin_size=.2)

    >>> # Add title
    >>> fig.update_layout(title='Dist Plot') # doctest: +SKIP
    >>> fig.show()


    Example 3: Plot with normal curve and hide rug plot

    >>> from plotly.figure_factory import create_distplot
    >>> import numpy as np

    >>> x1 = np.random.randn(190)
    >>> x2 = np.random.randn(200)+1
    >>> x3 = np.random.randn(200)-1
    >>> x4 = np.random.randn(210)+2

    >>> hist_data = [x1, x2, x3, x4]
    >>> group_labels = ['2012', '2013', '2014', '2015']

    >>> fig = create_distplot(
    ...     hist_data, group_labels, curve_type='normal',
    ...     show_rug=False, bin_size=.4)


    Example 4: Distplot with Pandas

    >>> from plotly.figure_factory import create_distplot
    >>> import numpy as np
    >>> import pandas as pd

    >>> df = pd.DataFrame({'2012': np.random.randn(200),
    ...                    '2013': np.random.randn(200)+1})
    >>> fig = create_distplot([df[c] for c in df.columns], df.columns)
    >>> fig.show()
    Nr
   ÚoverlayZclosestÚreversed)Z
traceorderg        r   Úy2F)ÚdomainÚanchorZzerolinegffffffÖ?é   Úfree)r   r   Úpositionr   g      Ð?Úx1)r   r   ZdtickZshowticklabels)ÚbarmodeÚ	hovermodeÚlegendÚxaxis1Úyaxis1Zyaxis2)r    r!   r"   r#   r$   )ÚdataÚlayout)r   r   Zvalidate_equal_lengthr   ÚfloatÚintÚlenÚ	_DistplotÚ	make_histÚappendÚmake_normalÚmake_kdeÚmake_rugr   ZLayoutÚdictÚsumZFigure)r   Úgroup_labelsÚbin_sizer   ÚcolorsÚrug_textÚhistnormÚ	show_histÚ
show_curveZshow_rugr%   ÚhistÚcurveÚrugr&   r   r   r   Úcreate_distplot4   sš    {
÷
÷
÷
÷
ú	û
r<   c                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )r*   z?
    Refer to TraceFactory.create_distplot() for docstring
    c
              
   C   sÎ   || _ || _|| _|| _|| _|	| _t|ƒ| _|r:|| _nd g| j | _g | _	g | _
|r`|| _nddddddddd	d
g
| _d g| j | _d g| j | _| j D ],}
| j	 t|
ƒd ¡ | j
 t|
ƒd ¡ qœd S )Nzrgb(31, 119, 180)zrgb(255, 127, 14)zrgb(44, 160, 44)zrgb(214, 39, 40)zrgb(148, 103, 189)zrgb(140, 86, 75)zrgb(227, 119, 194)zrgb(127, 127, 127)zrgb(188, 189, 34)zrgb(23, 190, 207)r   )r   r6   r2   r3   r7   r8   r)   Útrace_numberr5   ÚstartÚendr4   Úcurve_xÚcurve_yr,   ÚminÚmax)Úselfr   r6   r2   r3   r   r4   r5   r7   r8   Útracer   r   r   Ú__init__  s<    
ö
z_Distplot.__init__c                 C   sˆ   dg| j  }t| j ƒD ]l}td| j| dd| j| j| | j| t| j|t| jƒ  d�dt| j| | j	| | j
| d�dd	�||< q|S )
zŒ
        Makes the histogram(s) for FigureFactory.create_distplot().

        :rtype (list) hist: list of histogram representations
        NZ	histogramr   Úy1©ÚcolorF)r>   r?   Úsizegffffffæ?)ÚtypeÚxÚxaxisÚyaxisr6   ÚnameÚlegendgroupÚmarkerZautobinxZxbinsZopacity)r=   Úranger0   r   r6   r2   r4   r)   r>   r?   r3   )rD   r9   Úindexr   r   r   r+   A  s(    ýñz_Distplot.make_histc                    sè   dgˆj  }tˆj ƒD ]d‰ ‡ ‡fdd„tdƒD ƒˆjˆ < t ˆjˆ  ¡ˆjˆ  ƒˆjˆ < ˆjtkrˆjˆ   ˆj	ˆ  9  < qtˆj ƒD ]\‰ t
dˆjˆ  ˆjˆ  dddˆjˆ  ˆjˆ  ˆjr¾d	nd
t
ˆjˆ tˆjƒ  d�d�
|ˆ < q†|S )zÏ
        Makes the kernel density estimation(s) for create_distplot().

        This is called when curve_type = 'kde' in create_distplot().

        :rtype (list) curve: list of kde representations
        Nc                    s2   g | ]*}ˆj ˆ  |ˆjˆ  ˆj ˆ    d   ‘qS ©éô  ©r>   r?   ©Ú.0rL   ©rS   rD   r   r   Ú
<listcomp>g  s   ÿz&_Distplot.make_kde.<locals>.<listcomp>rU   Úscatterr   rG   ÚlinesFTrH   ©
rK   rL   ÚyrM   rN   ÚmoderO   rP   Ú
showlegendrQ   )r=   rR   r@   Úscipy_statsZgaussian_kder   rA   r6   ÚALTERNATIVE_HISTNORMr3   r0   r2   r7   r4   r)   )rD   r:   r   rY   r   r.   ]  s0    þÿ

öz_Distplot.make_kdec                    s$  dgˆj  }dgˆj  }dgˆj  }tˆj ƒD ]ˆ‰ tj ˆjˆ  ¡\|ˆ < |ˆ < ‡ ‡fdd„tdƒD ƒˆjˆ < tjjˆjˆ  |ˆ  |ˆ  d�ˆjˆ < ˆj	t
kr.ˆjˆ   ˆjˆ  9  < q.tˆj ƒD ]\‰ tdˆjˆ  ˆjˆ  ddd	ˆjˆ  ˆjˆ  ˆjrúd
ndtˆjˆ tˆjƒ  d�d�
|ˆ < qÂ|S )zÎ
        Makes the normal curve(s) for create_distplot().

        This is called when curve_type = 'normal' in create_distplot().

        :rtype (list) curve: list of normal curve representations
        Nc                    s2   g | ]*}ˆj ˆ  |ˆjˆ  ˆj ˆ    d   ‘qS rT   rV   rW   rY   r   r   rZ   �  s   ÿz)_Distplot.make_normal.<locals>.<listcomp>rU   )ÚlocÚscaler[   r   rG   r\   FTrH   r]   )r=   rR   ra   ZnormÚfitr   r@   ZpdfrA   r6   rb   r3   r0   r2   r7   r4   r)   )rD   r:   ZmeanÚsdr   rY   r   r-   �  s:    þ  ÿ
öz_Distplot.make_normalc                 C   s˜   dg| j  }t| j ƒD ]|}td| j| | j| gt| j| ƒ ddd| j| | j| | js`| jrddnd| j| t| j	|t| j	ƒ  dd	�d
�||< q|S )z{
        Makes the rug plot(s) for create_distplot().

        :rtype (list) rug: list of rug plot representations
        Nr[   r   r   ÚmarkersFTzline-ns-open)rI   Úsymbol)rK   rL   r^   rM   rN   r_   rO   rP   r`   ÚtextrQ   )
r=   rR   r0   r   r2   r)   r7   r8   r5   r4   )rD   r;   rS   r   r   r   r/   ©  s&     ÿõz_Distplot.make_rugN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__rF   r+   r.   r-   r/   r   r   r   r   r*     s   0$(r*   )Ú
__future__r   Zplotlyr   r   Zplotly.figure_factoryr   Zplotly.graph_objsr   Z
get_moduler   r   r   ra   ZDEFAULT_HISTNORMrb   r   r<   Úobjectr*   r   r   r   r   Ú<module>   s*   



%ö
 Y