L idZddlZddlZddlmZddlmZddlm Z ddl Z ddl Z ddlmZdZdd lmZdd lmZdd lmZdd lmZmZmZmZmZd dgZdZdZ dZ!dZ"GddZ#ddddddddddddddddddddZ$GddZ%ddddddddddZ&Gd d!eZ'dddddd"ddddddddd#d$d%dd&d'Z(y#e$rdZYwxYw)(z(Functions to visualize matrices of data.N)LineCollection)gridspec) hierarchyFT)cm)Grid) get_colormap)despineaxis_ticklabels_overlaprelative_luminanceto_utf8 _draw_figureheatmap clustermapct|tjr)djt t |j S|jS)z6Convert a pandas index or multiindex to an axis label.-) isinstancepd MultiIndexjoinmapr namesname)indexs T/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/seaborn/matrix.py_index_to_labelrs4%'xxGU[[122zzc t|tjr7|jDcgc]!}dj t t |#c}S|jScc}w)z5Convert a pandas index or multiindex into ticklabels.r)rrrvaluesrrr )ris r_index_to_ticklabelsr!'sD%'38<<@aWa)@@||As&Ac tjj} ||dtt ||S#t $r,|Dcgc]}tt ||ncc}wc}cYSwxYw)zAConvert either a list of colors or nested lists of colors to RGB.r)mplcolorsto_rgblistr ValueError)r$r% color_lists r_convert_colorsr)/sf ZZ  FHvayC'(( H@FG*S,-GGGHs< A1 A&% A10A1cD|$tj|jt}t |tj rV|j|jk7r t dtj||j|jt}nqt |tjrW|jj|js2|jj|jr d}t ||tj|z}|S)zEnsure that data and mask are compatible and add missing values. Values will be plotted for cells where ``mask`` is ``False``. ``data`` is expected to be a DataFrame; ``mask`` can be an array or a DataFrame. z&Mask must have the same shape as data.)rcolumnsdtypez2Mask must have the same index and columns as data.) npzerosshapeboolrndarrayr'r DataFramerr+equalsisnull)datamaskerrs r _matrix_maskr8<s |xx D)$ # :: #EF F||D"&**$(LL"&( D",, 'zz  ,||""4<<0FCS/ ! "))D/ !D Krc8eZdZdZ d dZdZdZdZdZdZ y) _HeatMapperz?Draw a heatmap plot of a matrix with nice labels and colormaps.Nc2t|tjr |j}n*t j |}tj|}t ||}tjjt j ||}d}t| tr| }t|j} n | durt|j} n| durg} d}t| tr| }t|j} n | durt|j} n| durg} t| sg|_g|_nVt| t r'| dk(r"d|_t|j|_n|j#| |\|_|_t| sg|_g|_nVt| t r'| dk(r"d|_t|j|_n|j#| |\|_|_t)|j}t)|j}||nd|_||nd|_|j/||||||||durd}d}nPt|t0r|}n;t j |}|j2|j2k7r d}t5|d}||_||_||_||_||_| in| jA|_!| |_"| i|_#y| jA|_#y)zInitialize the plotting object.rTFautoN(`data` and `annot` must have same shape.)$rrr2rr-asarrayr8ma masked_whereintr!r+rlenxticks xticklabelsstr _skip_ticksyticks yticklabelsrxlabelylabel_determine_cmap_paramsr0r/r'r5 plot_dataannot annot_datafmtcopy annot_kwscbarcbar_kws)selfr5vminvmaxcmapcenterrobustrNrPrRrSrTrErIr6rM xtickevery ytickeveryrJrKrOr7s r__init__z_HeatMapper.__init__ds dBLL ) I 4(I<< *DD$'EE&&rzz$'7C  k3 '$J.t||CIIrvvg&E99RVV$D1-Ityyq99H 6Fzz++FVOVf_MH!4,/JD$T4-B 11$))B-@DI II  c " ##E* ""4(- rc|j|jj\}}tjtj |dztj |dz\}}t |j|j|jj|j|jjD]\}}} } } | tjjus&t| } | dkDrdnd} d|jzdzj| }t| dd}|j!|j"|j$|||fi|y ) z/Add textual labels with the value in each cell.?g&1?z.15wz{:}rY)colorhavaN)update_scalarmappablerOr/r-meshgridarangezipflat get_arrayget_facecolorsr@maskedr rPformatdictupdaterRtext)rUaxmeshheightwidthxposyposxymrvallum text_color annotation text_kwargss r_annotate_heatmapz_HeatMapper._annotate_heatmaps! ""$-- [[5!1B!6 &8IB8NO d#&tyy$))'+~~'7'<'Q>Q>S'+';';$= 9 Aq!UC $(/&)DjUc "TXXo3;;C@ "XN ""4>>21j8K8 9rct|}|dk(rgg}}||fS|dk(rtj|dz|}}||fSd||}}}tj|||dz}||||}||fS)z3Return ticks and labels at evenly spaced intervals.rrr)rCr-r)rUlabels tickeverynticksstartendsteps rrGz_HeatMapper._skip_ticks s K >6Ef} !^IIaL2-v6E f} !!Y3EIIeS$/"4EE#dN+Ff}rc|jjj}|jj |}|j |j g|}|j|jg|}|jdg\}|jj}t||dz z} | dkrggfSt|| zdz} | dk(rdn| } |j|| \} }| |fS)z5Determine ticks and ticklabels that minimize overlap.rHr)figuredpi_scale_transinvertedget_window_extent transformedrrxaxisyaxis set_tickslabel1get_sizerBrCrG) rUrraxis transformbboxsizetickfontsize max_ticks tick_everyrs r _auto_ticksz_HeatMapper._auto_tickssII--668 ##%11)< DKK(."((#D)s#;;'')B/0 q=r6M[I-1 $/Qz ((< vf}rc t|dd|jd8|jd|j|jd|j|j |j fd|ji|}|jd|jjd fd|jjdf |j|jrq|jj|||fi|j}|j j#d|jd d r|j$j'dt)|j*t,r0|j*d k(r!|j/||j0d\}}n|j*|j0}}t)|j2t,r0|j2d k(r!|j/||j4d \}} n|j2|j4} }|j|||j7|} |j9| d} t;j<| dt?|jtA| rt;j<| dtA| rt;j<| d|j|jB|jD|jFr|jI||yy)z&Draw the heatmap on the provided Axes.T)rleftbottomnormNrVrWrXrr)xlimylim rasterizedFr<)rDrHverticalrotationrY)r horizontal)rJrK)%r get setdefaultrVrW pcolormeshrMrXsetr5r/ invert_yaxisrSrcolorbarrToutline set_linewidthsolidsset_rasterizedrrDrFrrErHrIset_xticklabelsset_yticklabelspltsetprr rJrKrNr) rUrcaxkwsrcbrDrErHrIxtlytls rplotz_HeatMapper.plot&sW 2D. 776? " NN6499 - NN6499 -r}}T^^C$))CsC Q *+1diiooa6H2IJ  99###D#rCT]]CB JJ $ $Q 'ww|U+ ((. dkk3 'DKK6,A"&"2"22t7G7G"K FK"&++t/?/?KF dkk3 'DKK6,A"&"2"22t7G7G"K FK"&++t/?/?KF fV,  -  z B " RYY "3 ' HHS: . "3 ' HHS< 0 dkk$++6 ::  " "2t , r)TTN) __name__ __module__ __qualname____doc__r]rLrrGrrrrr:r:as,I;?ZDx7)r9  :-rr:z.2gwhiter<)rVrWrXrYrZrNrPrR linewidths linecolorrSrTcbar_axsquarerErIr6rc t|||||||||| | |||}| |d<| |d<|tj}|r|jd|j || ||S)a Plot rectangular data as a color-encoded matrix. This is an Axes-level function and will draw the heatmap into the currently-active Axes if none is provided to the ``ax`` argument. Part of this Axes space will be taken and used to plot a colormap, unless ``cbar`` is False or a separate Axes is provided to ``cbar_ax``. Parameters ---------- data : rectangular dataset 2D dataset that can be coerced into an ndarray. If a Pandas DataFrame is provided, the index/column information will be used to label the columns and rows. vmin, vmax : floats, optional Values to anchor the colormap, otherwise they are inferred from the data and other keyword arguments. cmap : matplotlib colormap name or object, or list of colors, optional The mapping from data values to color space. If not provided, the default will depend on whether ``center`` is set. center : float, optional The value at which to center the colormap when plotting divergent data. Using this parameter will change the default ``cmap`` if none is specified. robust : bool, optional If True and ``vmin`` or ``vmax`` are absent, the colormap range is computed with robust quantiles instead of the extreme values. annot : bool or rectangular dataset, optional If True, write the data value in each cell. If an array-like with the same shape as ``data``, then use this to annotate the heatmap instead of the data. Note that DataFrames will match on position, not index. fmt : str, optional String formatting code to use when adding annotations. annot_kws : dict of key, value mappings, optional Keyword arguments for :meth:`matplotlib.axes.Axes.text` when ``annot`` is True. linewidths : float, optional Width of the lines that will divide each cell. linecolor : color, optional Color of the lines that will divide each cell. cbar : bool, optional Whether to draw a colorbar. cbar_kws : dict of key, value mappings, optional Keyword arguments for :meth:`matplotlib.figure.Figure.colorbar`. cbar_ax : matplotlib Axes, optional Axes in which to draw the colorbar, otherwise take space from the main Axes. square : bool, optional If True, set the Axes aspect to "equal" so each cell will be square-shaped. xticklabels, yticklabels : "auto", bool, list-like, or int, optional If True, plot the column names of the dataframe. If False, don't plot the column names. If list-like, plot these alternate labels as the xticklabels. If an integer, use the column names but plot only every n label. If "auto", try to densely plot non-overlapping labels. mask : bool array or DataFrame, optional If passed, data will not be shown in cells where ``mask`` is True. Cells with missing values are automatically masked. ax : matplotlib Axes, optional Axes in which to draw the plot, otherwise use the currently-active Axes. kwargs : other keyword arguments All other keyword arguments are passed to :meth:`matplotlib.axes.Axes.pcolormesh`. Returns ------- ax : matplotlib Axes Axes object with the heatmap. See Also -------- clustermap : Plot a matrix using hierarchical clustering to arrange the rows and columns. Examples -------- .. include:: ../docstrings/heatmap.rst r edgecolorequal)r:rgca set_aspectr)r5rVrWrXrYrZrNrPrRrrrSrTrrrErIr6rkwargsplotters rrrcsyv$dD&&%#T8[%t-G &F<#F; z WWY  g LLWf% IrcNeZdZdZdZdZdZedZdZ edZ dZ y ) _DendrogramPlotterzAObject for drawing tree of similarities between data rows/columnsc||_|jdk(r |j}t|tjr |j }n*t j|}tj|}||_||_ |jj|_ ||_ ||_ ||_||_ ||_||j|_n||_|j#|_dt j&|jjdzdz} |jrt)|jj*} |j,D cgc]} | |  } } |jrHg|_| |_g|_| |_t7|jj*|_d|_nu| |_g|_| |_g|_d|_t7|jj*|_n-ggc|_|_ggc|_|_d\|_|_|j$d|_|j$d |_ycc} w) zPlot a dendrogram of the relationships between the columns of data Parameters ---------- data : pandas.DataFrame Rectangular data rN rr=)r=r=dcoordicoord) rTrrr2rr-r?arrayr5r/metricmethodlabelrotatecalculated_linkagelinkagecalculate_dendrogram dendrogramrr!r reordered_indrDrHrErIrrKrJdependent_coordindependent_coord) rUr5rrrrrrrr ticklabelsr s rr]z_DendrogramPlotter.__init__s 99>66D dBLL )KKEJJt$E<<&D  YY__      ?22DL"DL335RYYtyyq122Q6 ::-diioo>J151C1CDA*Q-DJD{{  # #% #- -diioo>   #   #- #%   -diioo> ')2 $DK13R .D d.'- $DK#x8!%!:-Es Icrtj|j|j|j}|S)Nrr)rrrrr)rUrs r_calculate_linkage_scipyz+_DendrogramPlotter._calculate_linkage_scipys*##DJJt{{+/;;8rc:ddl}d}|jdk(xr|j|v}|s|jdk(r2|j|j|j|jS|j |j|j|j}|S)Nr)centroidmedianward euclideansingler) fastclusterrrlinkage_vectorrr)rUr euclidean_methodsr rs r_calculate_linkage_fastclusterz1_DendrogramPlotter._calculate_linkage_fastclusters;KK;.4;; 4  x/--djj59[[59[[.B B"))$**T[[15*>GNrc |jS#t$rKtj|jdk\rd}t j |Y|jSwxYw)Ni'zYClustering large matrix with scipy. Installing `fastcluster` may give better performance.)r ImportErrorr-prodr/warningswarnr)rUmsgs rrz%_DendrogramPlotter.calculated_linkage's^ #668 8 #wwtzz"e+D c",,..  #sAA&%A&cdtj|jdtj S)aCalculates a dendrogram based on the linkage matrix Made a separate function, not a property because don't want to recalculate the dendrogram every time it is accessed. Returns ------- dendrogram : dict Dendrogram dictionary as returned by scipy.cluster.hierarchy .dendrogram. The important key-value pairing is "reordered_ind" which indicates the re-ordering of the matrix T)no_plotcolor_threshold)rrrr-rmrUs rrz'_DendrogramPlotter.calculate_dendrogram4s(##DLL$57VVG= =rc |jdS)z2Indices of the matrix, reordered by the dendrogramleaves)rrs rrz _DendrogramPlotter.reordered_indDsx((rc x|in|j}|jdd|jd|jdd|jr0|jdk(r!t |j |j}n t |j|j }t|Dcgc]\}}tt ||c}}fi|}|j|t|j}ttt|j }|jrf|jj!d|j#d|dz|j%d|d z|j'|j)n*|j%d|dz|j#d|d zt+|d d |j-|j.|j0|j2|j4 |j7|j8} |j;|j<d } t?|j@t| dkDr"tC| rtEjF| dt| dkDr"tC| rtEjF| d |Scc}}w)zPlots a dendrogram of the similarities between data on the axes Parameters ---------- ax : matplotlib.axes.Axes Axes object upon which the dendrogram is plotted rrr$r)皙?rrrrightrg?Trrr)rDrHrJrKrrr)$rQrpoprrrrrrr&add_collectionrCrrorrset_ticks_positionset_ylimset_xlim invert_xaxisrr rrDrHrJrKrrErrIrrr rr) rUrtree_kwscoordsrrlinesnumber_of_leavesmax_dependent_coordrrs rrz_DendrogramPlotter.plotIs,")2x}}L"-Hhll7L&IJ ;;499>--t/E/EFF//1E1EFFFCDAqSAYC+!)+ % t112!#c4+?+?"@A ;; HH ' ' 0 KK+b0 1 KK.5 6 OO  OO  KK+b0 1 KK.5 62d. dkk$++kk$++  7  !1!12  !1!1J G RYY s8a<3C8 HHS< 0 s8a<3C8 HHS: . K Ds/J6 N) rrrrr]rrpropertyrrrrrrrrrsGK=;~ " / /= ))6rrr average)rrrrrrr'rc tr tdt|||||||} |tj}| j ||S)aDraw a tree diagram of relationships within a matrix Parameters ---------- data : pandas.DataFrame Rectangular data linkage : numpy.array, optional Linkage matrix axis : int, optional Which axis to use to calculate linkage. 0 is rows, 1 is columns. label : bool, optional If True, label the dendrogram at leaves with column or row names metric : str, optional Distance metric. Anything valid for scipy.spatial.distance.pdist method : str, optional Linkage method to use. Anything valid for scipy.cluster.hierarchy.linkage rotate : bool, optional When plotting the matrix, whether to rotate it 90 degrees counter-clockwise, so the leaves face right tree_kws : dict, optional Keyword arguments for the ``matplotlib.collections.LineCollection`` that is used for plotting the lines of the dendrogram tree. ax : matplotlib axis, optional Axis to plot on, otherwise uses current axis Returns ------- dendrogramplotter : _DendrogramPlotter A Dendrogram plotter object. Notes ----- Access the reordered dendrogram indices with dendrogramplotter.reordered_ind z)dendrogram requires scipy to be installed)rrrrrr)rr') _no_scipy RuntimeErrorrrrr) r5rrrrrrr'rrs rrrsSTFGG wT(.v',V=G z WWY <<2< 11rceZdZ d dZdZ ddZeddZeddZdZ eddZ d Z d Z d Z d Zy) ClusterGridNc jtr tdt|tjr||_ntj||_|j |j ||||_t|j||_ tj||_ |j||d\|_|_|j||d\|_|_ | \} } | \}}|j'|j| |}|j'|j | |}|j dnd}|jdnd}t)j*|||| |_|jj/|j,d |_|jj/|j,d |_|j0j5|j2j5d|_d|_|j-|jj/|j,d |_|j -|jj/|j,d |_|jj/|j,d|_| dx|_|_n>|jj/|j,d|_|j<|_| |_ d|_!d|_"y#t$$r| x} } Y.wxYw#t$$r| x}}Yr@r6dendrogram_ratio colors_ratiorNrow_dendrogram_ratiocol_dendrogram_ratiorow_colors_ratiocol_colors_ratior7r8nrowsncolss rr]zClusterGrid.__init__s KL L dBLL )DI T*DI&&tyy)W'57 !d3 zz'2   # #D*1 # = /.  # #D*1 # = /. K9I 6 "6 ?1= . .t';'79 (<(8: __,!__,!##E51=2?A"&!9!9$''%.!I!%!9!9$''%.!I ++- ++-!! ?? &!%!9!9" D  ?? &!%!9!9" D ,,224776?C  &* *DL48 <<33DGGDMBDL||DH  ""a K:J J #7 K  ?2> > / ?s$*L 0L! LL! L21L2cd}|@t|tjtjfr t |ds|dk(st |ds|dk(r|rdnd}|d|d }t ||dk(r|j |j}n|j |j}|jtjd }t|tjr,t|j}|jj}n)|jd g}n |jg}|j}t!|}||fS) zAPreprocess {row/col}_colors to extract labels and convert colors.Nrrr+rcolrowz<_colors indices can't be matched with data indices. Provide z?_colors as a non-indexed datatype, e.g. by using `.to_numpy()``rr=)rrr2SerieshasattrrBreindexrr+rbobjectfillnar&rrrr))rUr5r$rr axis_namers rr=zClusterGrid._preprocess_colorss$  &2<<";< g.419i0TQY)-5I'[)//8k:DDC$C.(19#^^DJJ7F#^^DLL9F v.55g>fbll3!&..1F#XX__F{{*"$"(++#]]F$V,Fv~rc||jdi|}n|}| | td||j||}||j||}|S)z,Extract variables from data or use directly.z:Cannot perform both z-scoring and standard-scaling on datar)pivotr'rRrS)rUr5rQrRrSr;s rr:zClusterGrid.format_data0st  TZZ,),FF  >#=LN N  \\&'2F  %((@F rc|dk(r|}n |j}||jz |jz }|dk(r|S|jS)aStandarize the mean and variance of the data axis Parameters ---------- data2d : pandas.DataFrame Data to normalize axis : int Which axis to normalize across. If 0, normalize across rows, if 1, normalize across columns. Returns ------- normalized : pandas.DataFrame Noramlized data with a mean of 0 and variance of 1 across the specified axis. r)rmeanstd)r;rz_scoreds rrRzClusterGrid.z_scoreDsJ$ 19HxxHx}}.(,,.@ 19O:: rc|dk(r|}n |j}|j}||z |j|jz z }|dk(r|S|jS)aDivide the data by the difference between the max and min Parameters ---------- data2d : pandas.DataFrame Data to normalize axis : int Which axis to normalize across. If 0, normalize across rows, if 1, normalize across columns. Returns ------- standardized : pandas.DataFrame Noramlized data with a mean of 0 and variance of 1 across the specified axis. r)rminro)r;r standardizedsubtracts rrSzClusterGrid.standard_scalebsi( 19!L!88L##%$x/    !1!1!3 35  19 >> !rc|g}|/tj|dkDr t|}nd}|||zgz }|jdt |z |S)z8Get the proportions of the figure taken up by each axes.r_r)r-ndimrCappendsum)rUr$rTrUratiosn_colorss rrCzClusterGrid.dim_ratioss["#  wwv"v; x,./ /F  a#f+o& rcZ tjj|ddt|c} |g}i}t j| ft}t|D]:\}}t|D]'\}} |j| t|} | |||f<)<|dd|f}|dk(r |j}tjjt|} || fS#t$r?t|t|dc} t fd|ddDs t dYwxYw)aTurns a list of colors into a numpy matrix and matplotlib colormap These arguments can now be plotted using heatmap(matrix, cmap) and the provided colors will be plotted. Parameters ---------- colors : list of matplotlib colors Colors to label the rows or columns of a dataframe. ind : list of ints Ordering of the rows or columns, to reorder the original colors by the clustered dendrogram order axis : int Which axis this is labeling Returns ------- matrix : numpy.array A numpy array of integer values, where each indexes into the cmap cmap : matplotlib.colors.ListedColormap rrc3:K|]}t|k(yw)N)rC).0crs r z.s7qs1v{7sNz/Multiple side color vectors must have same size)r#r$r%rCr'allr-r.rB enumeraterrrkr&) r$indrr unique_colorsmatrixr innerjridxrXrs @rcolor_list_to_matrix_and_cmapz)ClusterGrid.color_list_to_matrix_and_cmaps60  JJ  fQi (c&kDAqXF 1a&#&!&) #HAu%e, #5#..uc-6HI"q!t  # # 3 19XXFzz((m)<=t|1 Tv;F1IDAq7F12J77 !RSS8 Ts"C""AD*)D*c |r.t|j||dd|jd|| |_n6|jj g|jj g|r-t|j||dd|j |||_n6|j j g|j j gt|jddt|j ddy)NFrT)rrrrrrrr'r)rrrrrrr'r ) rr;rGrO set_xticks set_yticksrHrPr )rU row_cluster col_clusterrr row_linkage col_linkager's rplot_dendrogramszClusterGrid.plot_dendrogramss ", F6Q))$ !#D   " " - -b 1  " " - -b 1 ", F6411;!#D   " " - -b 1  " " - -b 14))$TB4))$TBrc r|j}|jdd|jdd|jdd|jdd|jdd|jdd|jdd|jd d|jd d|j|j|j|d \}}|j |j}nd }t |f|d |j |d d||d urHtj|j jdnt|j dd|j|j|j|d \}}|j |j}nd }t |f|d |jd |d||d urT|jjjtj|jj!d yyt|jddy)zPlots color labels between the dendrogram and the heatmap Parameters ---------- heatmap_kws : dict Keyword arguments heatmap rXNrrYrNrVrWrZrErIrr5F)rXrSrrErIZrT)rrr)rQr!r>rr?rrJrrget_xticklabelsr r@rArKr tick_rightget_yticklabels)rUxindyindrr~rXr?rAs r plot_colorszClusterGrid.plot_colorsshhj   $    $  t$  t$ ?? &==A>/LFD$$0#'#8#8 #( F LEd6H6H 0e LGJ L u,++;;=K D&&T$ ? ?? &==A>/LFD$$0#'#8#8 #( F LEd6H6H %3C LGJ L u,""((335++;;=J- D&&T$ ?rc |jj||f|_|jj||f|_|jdd} t j ||}|jdd} t j ||}|jdd}||durnpt|tr |j}nQt j |}|j|jjk7r d} t| ||dd|f}|}|jd|jdut|jf|j|j||j|||d||jj!}|sdn|d j#} |jj$j'd |jj$j)d | 1|jj!}t+j,|| t/d d } |j|j0j2di| y|jj5|j0j2di| |jj7|jj9|j:y#t tf$rYzwxYw#t tf$rYewxYw)NrEr<rIrNFr>rS)rrrTr6rErIrNrrr{Gz?)h_padw_padr)r;ilocr6r!r-r?rB IndexErrorrr0r/r'rrMrrLr get_rotationrr#set_label_positionrrrr< tight_layoutrI set_axis_on set_positionrN) rU colorbar_kwsrrrrrrNrOr7ytl_rot tight_paramss r plot_matrixzClusterGrid.plot_matrix$skk&&tTz2 IINN4:. ggmV, **S/$'CggmV, **S/$'C & =EUN %&![[ ZZ. ##t{{'8'88DC$S/)'-ag6 E vt||478  F%DIIS FAD Foo--/!$s1v':':'< 009 009  //113C HHS7 +#S1 <<  %DLL % % 5 5 LL % % ' %DLL % % 5 5 LL $ $ & LL % %dmm 4_:&    :&   s$K?K*K'&K'*K=<K=c P| jddr(d} tj| | jd|in|}|j ||||||| |j j } |jj } |j| | fi| |j|| | fi| |S#t$r/tj|jjd} YywxYw#t$r/tj|jjd} YwxYw)NrFz%``square=True`` ignored in clustermap)rrr'rr)rrrr!rrPrAttributeErrorr-rr;r/rOrr) rUrrrrrrrr'rrrrs rrzClusterGrid.plot]s 778U #9C MM#  GGH )1r|  k;*5;'/  1 3&&44D 3&&44D t+s+tT9S9  399T[[..q12D 3 399T[[..q12D 3s$B21C-25C*)C*-5D%$D%) NNNNNNNNNN)NN)r)r)rrrr]r=r: staticmethodrRrSrCrrrrrrrrr2r2sJNFJDHJ#X(T48#'(:""B$11fC2?@B75rrr2)rrrgQ?)rg?g?g ףp= ?)rQrrrRrSr4rTrrrrr>r@r6rTrUrNr'c tr tdt|||| | |||||| }|jd||||| | | |d|S)a0 Plot a matrix dataset as a hierarchically-clustered heatmap. This function requires scipy to be available. Parameters ---------- data : 2D array-like Rectangular data for clustering. Cannot contain NAs. pivot_kws : dict, optional If `data` is a tidy dataframe, can provide keyword arguments for pivot to create a rectangular dataframe. method : str, optional Linkage method to use for calculating clusters. See :func:`scipy.cluster.hierarchy.linkage` documentation for more information. metric : str, optional Distance metric to use for the data. See :func:`scipy.spatial.distance.pdist` documentation for more options. To use different metrics (or methods) for rows and columns, you may construct each linkage matrix yourself and provide them as `{row,col}_linkage`. z_score : int or None, optional Either 0 (rows) or 1 (columns). Whether or not to calculate z-scores for the rows or the columns. Z scores are: z = (x - mean)/std, so values in each row (column) will get the mean of the row (column) subtracted, then divided by the standard deviation of the row (column). This ensures that each row (column) has mean of 0 and variance of 1. standard_scale : int or None, optional Either 0 (rows) or 1 (columns). Whether or not to standardize that dimension, meaning for each row or column, subtract the minimum and divide each by its maximum. figsize : tuple of (width, height), optional Overall size of the figure. cbar_kws : dict, optional Keyword arguments to pass to `cbar_kws` in :func:`heatmap`, e.g. to add a label to the colorbar. {row,col}_cluster : bool, optional If ``True``, cluster the {rows, columns}. {row,col}_linkage : :class:`numpy.ndarray`, optional Precomputed linkage matrix for the rows or columns. See :func:`scipy.cluster.hierarchy.linkage` for specific formats. {row,col}_colors : list-like or pandas DataFrame/Series, optional List of colors to label for either the rows or columns. Useful to evaluate whether samples within a group are clustered together. Can use nested lists or DataFrame for multiple color levels of labeling. If given as a :class:`pandas.DataFrame` or :class:`pandas.Series`, labels for the colors are extracted from the DataFrames column names or from the name of the Series. DataFrame/Series colors are also matched to the data by their index, ensuring colors are drawn in the correct order. mask : bool array or DataFrame, optional If passed, data will not be shown in cells where `mask` is True. Cells with missing values are automatically masked. Only used for visualizing, not for calculating. {dendrogram,colors}_ratio : float, or pair of floats, optional Proportion of the figure size devoted to the two marginal elements. If a pair is given, they correspond to (row, col) ratios. cbar_pos : tuple of (left, bottom, width, height), optional Position of the colorbar axes in the figure. Setting to ``None`` will disable the colorbar. tree_kws : dict, optional Parameters for the :class:`matplotlib.collections.LineCollection` that is used to plot the lines of the dendrogram tree. kwargs : other keyword arguments All other keyword arguments are passed to :func:`heatmap`. Returns ------- :class:`ClusterGrid` A :class:`ClusterGrid` instance. See Also -------- heatmap : Plot rectangular data as a color-encoded matrix. Notes ----- The returned object has a ``savefig`` method that should be used if you want to save the figure object without clipping the dendrograms. To access the reordered row indices, use: ``clustergrid.dendrogram_row.reordered_ind`` Column indices, use: ``clustergrid.dendrogram_col.reordered_ind`` Examples -------- .. include:: ../docstrings/clustermap.rst z)clustermap requires scipy to be available) rQr4r>r@rRrSr6rTrUrN)rrrrrrrr'r)r/r0r2r)r5rQrrrRrSr4rTrrrrr>r@r6rTrUrNr'rrs rrrzsrNFGG$)W%/J").#6F'3h HG 7<< 5vf%-$/[$/[!)  5.4  55r))rr matplotlibr#matplotlib.collectionsrmatplotlib.pyplotpyplotrrnumpyr-pandasr scipy.clusterrr/rr=raxisgridr_compatr utilsr r r r r__all__rr!r)r8r:rrrr2rrrrrs!.1'I! l # H"J--H DtD ETG d f& $iXppj q[ UTd32l$H9[ x t$4d 4t5[#IsB99CC