
    6DhP                     "   S SK Jr  S SKJrJr  S SKJr  S SKJ	r	  S SK
Jr  \R                  " S5      r\R                  " S5      r\R                  " S5      rS	 rSS
 jrSS jrS rS rS rS rS rSS jrS rS rS r          SS jrg)    )Number)
exceptionsoptional_importsN)
graph_objs)make_subplotspandasnumpyzscipy.statsc                    [         R                  U [        5      n[         R                  U5      n[         R	                  U5      n[         R                  USSS9n[         R                  USSS9n[         R                  USSS9nXe-
  nSU-  n[         R                  XXX-
  :     5      n	[         R	                  XXh-   :*     5      n
UUUUUU	U
S	.$ )
z.
Calculate statistics for use in violin plot.
2   linear)interpolation   lowerK   higher      ?)minmaxq1q2q3d1d2)npasarrayfloatr   r   
percentile)dataxvals_minvals_maxr   r   r   iqrwhisker_distr   r   s              O/var/www/html/env/lib/python3.13/site-packages/plotly/figure_factory/_violin.py
calc_statsr%      s     	

4AvvayHvvayH	q"H	5B	q"G	4B	q"H	5B
'C9L 
)*+	,B	)*+	,B     c                 $   [        [        U 5      5       Vs/ s H3  nSSR                  X   5      -   S-   SR                  X   5      -   S-   PM5     nn[        R                  " U USSUSU[        R
                  R                  SUS	S
9SSS9
$ s  snf )z?
Produces a sideways probability distribution fig violin plot.
z(pdf(y), y)=({:0.2f}z, )lines tonextxg      ?spline)widthcolorshapetext)
r   ymodenamer1   fill	fillcolorline	hoverinfoopacity)rangelenformatr   ScatterscatterLine)r   r2   r6   	linecolorir1   s         r$   make_half_violinrB   )   s     s1vA 	)**140047):J:J14:PPSVV 	 
 

$$3ix$P s   :Bc           
          [         R                  " U U* U-
  /[        U 5      -  [         R                  R	                  USS9SSSSS9$ )z*
Returns a rugplot fig for a violin plot.
zline-ew-open)r/   symbolmarkersr+   Fr2   )r2   r   markerr3   r4   
showlegendr8   )r   r=   r;   r>   Marker)valspdf_maxdistancer/   s       r$   make_violin_rugplotrL   @   sU     
8h
#d)
+!!((u^(L r&   c                 p    [         R                  " SS/X/SS[         R                  R                  SSS9S9$ )z7
Returns the scatterplot fig of most of a violin plot.
r   r+   r*   r   
rgb(0,0,0)r.   r/   )r   r2   r4   r3   r7   )r   r=   r>   r?   )r   r   s     r$   make_non_outlier_intervalrP   O   sB     a&($$3l$C r&   c                     [         R                  " SS/X/SSR                  U 5      -   SSR                  U5      -   /S[         R                  R	                  SSS9S	S
9$ )z8
Makes the upper and lower quartiles for a violin plot.
r   zlower-quartile: r(   zupper-quartile: r*      rN   rO   r1   )r   r2   r1   r3   r7   r8   )r   r=   r<   r>   r?   )r   r   s     r$   make_quartilesrS   \   so     a&(!1!1"!55!1!1"!55
 $$1L$A
 
r&   c                 n    [         R                  " S/U /SSR                  U 5      -   /S[        SSS9SS	9$ )
z3
Formats the 'median' hovertext for a violin plot.
r   zmedian: r(   rE   squarezrgb(255,255,255))rD   r/   r1   )r   r2   r1   r3   rF   r8   )r   r=   r<   dict)r   s    r$   make_medianrW   m   sH     #$9++B//08+=> r&   c                 N    [         R                  R                  U USSSSSSS9nU$ )z%
Makes the x-axis for a violin plot.
Fr+   )titler:   showgridzerolineshowlinemirrorticksshowticklabels)r   layoutXAxis)xaxis_titlexaxis_rangexaxiss      r$   
make_XAxisre   {   s>     ## $ 	E Lr&   c                 N    [         R                  R                  U SSSSSSSS9nU$ )z%
Makes the y-axis for a violin plot.
TrR   F)rY   r_   	autorangeticklenr\   r[   rZ   r]   )r   r`   YAxis)yaxis_titleyaxiss     r$   
make_YAxisrl      s>     ## $ 	E Lr&   c           	      v   [         R                  U [        5      n [        U 5      S   n[        U 5      S   n[        U 5      S   n[        U 5      S   n[        U 5      S   n[        U 5      S   n[        U 5      S   n	[        R                  U 5      n
[         R                  X4S5      nU
" U5      n[         R                  U5      nU(       a  S	U-  S
-  OSnU* U-
  S-
  US-   /n[        U* XS9[        XUS9[        X5      [        XW5      [        U5      /nU(       a  UR                  [        XXS95        UU4$ )z7
Refer to FigureFactory.create_violin() for docstring.
r   r   r   r   r   r   r   d   g       @
   r   g?)r6   )rK   r/   )r   r   r   r%   scipy_statsgaussian_kdelinspacer   rB   rP   rS   rW   appendrL   )rI   r6   rugplotr    r!   r   r   r   r   r   pdfxxyymax_pdfrK   plot_xrange	plot_datas                    r$   
violinplotr{      s?    ::dE"D$&H$&H	D	$	B	D	$	B	D	$	B	D	$	B	D	$	B 
"
"4
(C	X	-B	RBffRjG'.g#AH8h&,gm<K"b695!")rBI R	
 k!!r&   c           	         / nX    H  nX;  d  M
  UR                  U5        M     U(       a  UR                  5         U R                  U/5      n[        U5      n[	        SUSSSS9nSn[        U5       H  u  nn[        R                  UR                  U5      U   [        5      nU[        U5      :  a  Sn[        UUU   US9u  nn[        R                  " 5       nU H  nUR                  USUS-   5        M     US-  nUS   R                  S	R                  US-   5      [!        UU   U5      05        M     US   R                  S
R                  S5      [#        S5      05        US   R                  U
SSSUU	S9  U$ )i
Refer to FigureFactory.create_violin() for docstring.

Returns fig for violin plot without colorscale.

   T皙?Frowscolsshared_yaxeshorizontal_spacing
print_gridr   r6   rt   r`   xaxis{}yaxis{}r+   closestrY   rG   	hovermodeautosizeheightr.   )rs   sortgroupbyr;   r   	enumerater   r   	get_groupr   r{   r   Layoutappend_traceupdater<   re   rl   )r   data_headergroup_headercolorsuse_colorscalegroup_statsrt   r   r   r.   rY   
group_namer4   gbLfigcolor_indexkgrrI   rz   ry   r`   items                           r$   violin_no_colorscaler      s   * J"!d# # 	|n	%BJA
QTePUC K:&2zz",,r*;7?#f+%K!+F;/"
	; ""$DT1a!e, q 	Ha!e$jA&LM	
 '& M)**1-z"~>?M   Jr&   c                    / nX    H  nX;  d  M
  UR                  U5        M     U(       a  UR                  5         U H  nX;  d  M
  [        R                  " S5      e   U R	                  U/5      n[        U5      n[        SUSSSS9n[        R                  " US   [        R                  5      n[        R                  " US   [        R                  5      n/ nU H  nUR                  UU   5        M     [        U5      n[        U5      n[        U5       H  u  nn[        R                  UR                  U5      U   [         5      nUU   U-
  UU-
  -  n[        R"                  " UUU5      n[%        USR'                  U5      US	9u  nn[(        R*                  " 5       nU H  nUR-                  USUS-   5        M     US
   R/                  SR'                  US-   5      [1        UU   U5      05        M     [(        R2                  " S/S/S[5        SUUSUS   /SUS   //SS9SS9n UR-                  U SU5        US
   R/                  SR'                  S5      [7        S5      05        US
   R/                  U
SSSUU	S9  U$ )zf
Refer to FigureFactory.create_violin() for docstring.

Returns fig for violin plot with colorscale.

zRAll values/groups in the index column must be represented as a key in group_stats.r~   Tr   Fr   r   zrgb{}r   r`   r   rE      )sizecmincmax
colorscale	showscale)r   r2   r3   rF   rG   r   r+   r   r   )rs   r   r   PlotlyErrorr   r;   r   clrscolor_parserunlabel_rgbr   r   r   r   r   r   r   find_intermediate_colorr{   r<   r   r   r   r   re   r=   rV   rl   )!r   r   r   r   r   r   rt   r   r   r.   rY   r   r4   groupr   r   r   lowcolor	highcolorgroup_stats_valueskey	max_value	min_valuer   r   rI   intermedintermed_colorrz   ry   r`   r   trace_dummys!                                    r$   violin_colorscaler     s   * J"!d# #  #((+   
|n	%BJA
QTePUC
   D,<,<=H!!&)T-=-=>I !!+c"23  &'I&'I:&2zz",,r*;7?  Oi/I	4IJ55h	8T!+GNN>:G"
	; ""$DT1a!e, Ha!e$jA&LM	
 '$ $$##F1IF1I7
 K [!Q' M)**1-z"~>?M   Jr&   c           	         / nX    H  nX;  d  M
  UR                  U5        M     U(       a  UR                  5         U H  nX;  d  M
  [        R                  " S5      e   U R	                  U/5      n[        U5      n[        SUSSSS9n[        U5       H  u  nn[        R                  UR                  U5      U   [        5      n[        UUU   US9u  nn[        R                  " 5       nU H  nUR                  USUS-   5        M     US   R!                  S	R#                  US-   5      [%        UU   U5      05        M     US   R!                  S
R#                  S5      ['        S5      05        US   R!                  U
SSSUU	S9  U$ )r}   zMIf colors is a dictionary, all the group names must appear as keys in colors.r~   Tr   Fr   r   r`   r   r   r+   r   r   )rs   r   r   r   r   r;   r   r   r   r   r   r   r{   r   r   r   r   r<   re   rl   )r   r   r   r   r   r   rt   r   r   r.   rY   r   r4   r   r   r   r   r   r   rI   rz   ry   r`   r   s                           r$   violin_dictr   n  s   * J"!d# #  (("   
|n	%BJA
QTePUC :&2zz",,r*;7?!+DF2JPW!X	;""$DT1a!e,  	Ha!e$jA&LM	
 ' M)**1-z"~>?M   Jr&   c                    [        U[        5      (       a  [        R                  " US5      nO[        R                  " US5      nUGcf  [        U [
        5      (       aR  [        U 5      S::  a  [        R                  " S5      e[        S U  5       5      (       d  [        R                  " S5      e[        (       ah  [        U [        R                  R                  R                  5      (       a5  Uc  [        R                  " S5      eX   R                  R                  5       n [!        XS   US9u  p["        R$                  " U
S["        R&                  R)                  S	S
9USU	[+        SU5      [-        S5      SS9	nUS   R/                  [        SSSS95        ["        R0                  " XS9nU$ [        U [        R                  R                  R                  5      (       d  [        R                  " S5      eUc  [        R                  " S5      eUSL aC  [        U[        5      (       a  [3        U UUUUUUUUU	U
5      nU$ [5        U UUUUUUUUU	U
5      nU$ [        U[        5      (       a  [        R                  " S5      e[        U5      S:  a  [        R                  " S5      e[        U[        5      (       d  [        R                  " S5      e[7        U UUUUUUUUU	U
5      nU$ )a  
**deprecated**, use instead the plotly.graph_objects trace
:class:`plotly.graph_objects.Violin`.

:param (list|array) data: accepts either a list of numerical values,
    a list of dictionaries all with identical keys and at least one
    column of numeric values, or a pandas dataframe with at least one
    column of numbers.
:param (str) data_header: the header of the data column to be used
    from an inputted pandas dataframe. Not applicable if 'data' is
    a list of numeric values.
:param (str) group_header: applicable if grouping data by a variable.
    'group_header' must be set to the name of the grouping variable.
:param (str|tuple|list|dict) colors: either a plotly scale name,
    an rgb or hex color, a color tuple, a list of colors or a
    dictionary. An rgb color is of the form 'rgb(x, y, z)' where
    x, y and z belong to the interval [0, 255] and a color tuple is a
    tuple of the form (a, b, c) where a, b and c belong to [0, 1].
    If colors is a list, it must contain valid color types as its
    members.
:param (bool) use_colorscale: only applicable if grouping by another
    variable. Will implement a colorscale based on the first 2 colors
    of param colors. This means colors must be a list with at least 2
    colors in it (Plotly colorscales are accepted since they map to a
    list of two rgb colors). Default = False
:param (dict) group_stats: a dictionary where each key is a unique
    value from the group_header column in data. Each value must be a
    number and will be used to color the violin plots if a colorscale
    is being used.
:param (bool) rugplot: determines if a rugplot is draw on violin plot.
    Default = True
:param (bool) sort: determines if violins are sorted
    alphabetically (True) or by input order (False). Default = False
:param (float) height: the height of the violin plot.
:param (float) width: the width of the violin plot.
:param (str) title: the title of the violin plot.

Example 1: Single Violin Plot

>>> from plotly.figure_factory import create_violin
>>> import plotly.graph_objs as graph_objects

>>> import numpy as np
>>> from scipy import stats

>>> # create list of random values
>>> data_list = np.random.randn(100)

>>> # create violin fig
>>> fig = create_violin(data_list, colors='#604d9e')

>>> # plot
>>> fig.show()

Example 2: Multiple Violin Plots with Qualitative Coloring

>>> from plotly.figure_factory import create_violin
>>> import plotly.graph_objs as graph_objects

>>> import numpy as np
>>> import pandas as pd
>>> from scipy import stats

>>> # create dataframe
>>> np.random.seed(619517)
>>> Nr=250
>>> y = np.random.randn(Nr)
>>> gr = np.random.choice(list("ABCDE"), Nr)
>>> norm_params=[(0, 1.2), (0.7, 1), (-0.5, 1.4), (0.3, 1), (0.8, 0.9)]

>>> for i, letter in enumerate("ABCDE"):
...     y[gr == letter] *=norm_params[i][1]+ norm_params[i][0]
>>> df = pd.DataFrame(dict(Score=y, Group=gr))

>>> # create violin fig
>>> fig = create_violin(df, data_header='Score', group_header='Group',
...                    sort=True, height=600, width=1000)

>>> # plot
>>> fig.show()

Example 3: Violin Plots with Colorscale

>>> from plotly.figure_factory import create_violin
>>> import plotly.graph_objs as graph_objects

>>> import numpy as np
>>> import pandas as pd
>>> from scipy import stats

>>> # create dataframe
>>> np.random.seed(619517)
>>> Nr=250
>>> y = np.random.randn(Nr)
>>> gr = np.random.choice(list("ABCDE"), Nr)
>>> norm_params=[(0, 1.2), (0.7, 1), (-0.5, 1.4), (0.3, 1), (0.8, 0.9)]

>>> for i, letter in enumerate("ABCDE"):
...     y[gr == letter] *=norm_params[i][1]+ norm_params[i][0]
>>> df = pd.DataFrame(dict(Score=y, Group=gr))

>>> # define header params
>>> data_header = 'Score'
>>> group_header = 'Group'

>>> # make groupby object with pandas
>>> group_stats = {}
>>> groupby_data = df.groupby([group_header])

>>> for group in "ABCDE":
...     data_from_group = groupby_data.get_group(group)[data_header]
...     # take a stat of the grouped data
...     stat = np.median(data_from_group)
...     # add to dictionary
...     group_stats[group] = stat

>>> # create violin fig
>>> fig = create_violin(df, data_header='Score', group_header='Group',
...                     height=600, width=1000, use_colorscale=True,
...                     group_stats=group_stats)

>>> # plot
>>> fig.show()
rgbr   zRIf data is a list, it must be nonempty and contain either numbers or dictionaries.c              3   B   #    U  H  n[        U[        5      v   M     g 7f)N)
isinstancer   ).0elements     r$   	<genexpr> create_violin.<locals>.<genexpr>P  s     G$wz'622$s   z0If data is a list, it must contain only numbers.zVdata_header must be the column name with the desired numeric data for the violin plot.r   F   )r   r+   r   )	rY   r   fontr   rG   r.   rd   rk   r   rk   )r\   r_   r^   )r   r`   zGError. You must use a pandas DataFrame if you are using a group header.zFThe colors param cannot be a dictionary if you are using a colorscale.r   zHcolors must be a list with at least 2 colors. A Plotly scale is allowed.z,Your group_stats param must be a dictionary.)r   rV   r   validate_colors_dictvalidate_colorslistr;   r   r   allpdcoreframe	DataFramevaluestolistr{   r   r   r`   Fontre   rl   r   Figurer   r   r   )r   r   r   r   r   r   rt   r   r   r.   rY   valid_colorsrz   ry   r`   r   s                   r$   create_violinr     s   V &$00?++FE: dD!!4yA~ ,,/  G$GGG ,,I  2*T277==#:#:;;" ,,'  $++224D ",OW"
	 """"''R'0R-R.

 	wtU5PRSTY>
 $ 7 788((   ((,  U",--!  " 
*  " 
,-- ,,*  < 1$ ,,/  k400 ,,E  $C Jr&   )#1f77b4zrgb(0, 0, 0))r   )r   T)
NNNFNTFi  iX  zViolin and Rug Plot)numbersr   plotlyr   r   plotly.colorsr   r   plotly.graph_objsr   plotly.subplotsr   
get_moduler   r   rp   r%   rB   rL   rP   rS   rW   re   rl   r{   r   r   r   r    r&   r$   <module>r      s     /  ( )  *  )))-88.
"""$"N@FdNET 	

Pr&   