@@ -6051,14 +6051,18 @@ def create_distplot(hist_data, group_labels,
6051
6051
6052
6052
@staticmethod
6053
6053
def create_dendrogram (X , orientation = "bottom" , labels = None ,
6054
- colorscale = None ):
6054
+ colorscale = None , distfun = scs .distance .pdist ,
6055
+ linkagefun = lambda x : sch .linkage (x , 'complete' )):
6055
6056
"""
6056
6057
BETA function that returns a dendrogram Plotly figure object.
6057
6058
6058
6059
:param (ndarray) X: Matrix of observations as array of arrays
6059
6060
:param (str) orientation: 'top', 'right', 'bottom', or 'left'
6060
6061
:param (list) labels: List of axis category labels(observation labels)
6061
6062
:param (list) colorscale: Optional colorscale for dendrogram tree
6063
+ :param (function) distfun: Function to compute the pairwise distance from the observations
6064
+ :param (function) linkagefun: Funktion to compute the linkage matrix from the pairwise distances
6065
+
6062
6066
clusters
6063
6067
6064
6068
Example 1: Simple bottom oriented dendrogram
@@ -6114,7 +6118,8 @@ def create_dendrogram(X, orientation="bottom", labels=None,
6114
6118
if len (s ) != 2 :
6115
6119
exceptions .PlotlyError ("X should be 2-dimensional array." )
6116
6120
6117
- dendrogram = _Dendrogram (X , orientation , labels , colorscale )
6121
+ dendrogram = _Dendrogram (X , orientation , labels , colorscale ,
6122
+ distfun = distfun , linkagefun = linkagefun )
6118
6123
6119
6124
return {'layout' : dendrogram .layout ,
6120
6125
'data' : dendrogram .data }
@@ -7041,7 +7046,8 @@ class _Dendrogram(FigureFactory):
7041
7046
"""Refer to FigureFactory.create_dendrogram() for docstring."""
7042
7047
7043
7048
def __init__ (self , X , orientation = 'bottom' , labels = None , colorscale = None ,
7044
- width = "100%" , height = "100%" , xaxis = 'xaxis' , yaxis = 'yaxis' ):
7049
+ width = "100%" , height = "100%" , xaxis = 'xaxis' , yaxis = 'yaxis' ,
7050
+ distfun = scs .distance .pdist , linkagefun = lambda x : sch .linkage (x , 'complete' )):
7045
7051
# TODO: protected until #282
7046
7052
from plotly .graph_objs import graph_objs
7047
7053
self .orientation = orientation
@@ -7064,7 +7070,7 @@ def __init__(self, X, orientation='bottom', labels=None, colorscale=None,
7064
7070
self .sign [self .yaxis ] = - 1
7065
7071
7066
7072
(dd_traces , xvals , yvals ,
7067
- ordered_labels , leaves ) = self .get_dendrogram_traces (X , colorscale )
7073
+ ordered_labels , leaves ) = self .get_dendrogram_traces (X , colorscale , distfun , linkagefun )
7068
7074
7069
7075
self .labels = ordered_labels
7070
7076
self .leaves = leaves
@@ -7173,12 +7179,14 @@ def set_figure_layout(self, width, height):
7173
7179
7174
7180
return self .layout
7175
7181
7176
- def get_dendrogram_traces (self , X , colorscale ):
7182
+ def get_dendrogram_traces (self , X , colorscale , distfun , linkagefun ):
7177
7183
"""
7178
7184
Calculates all the elements needed for plotting a dendrogram.
7179
7185
7180
7186
:param (ndarray) X: Matrix of observations as array of arrays
7181
7187
:param (list) colorscale: Color scale for dendrogram tree clusters
7188
+ :param (function) distfun: Function to compute the pairwise distance from the observations
7189
+ :param (function) linkagefun: Funktion to compute the linkage matrix from the pairwise distances
7182
7190
:rtype (tuple): Contains all the traces in the following order:
7183
7191
(a) trace_list: List of Plotly trace objects for dendrogram tree
7184
7192
(b) icoord: All X points of the dendrogram tree as array of arrays
@@ -7192,8 +7200,8 @@ def get_dendrogram_traces(self, X, colorscale):
7192
7200
"""
7193
7201
# TODO: protected until #282
7194
7202
from plotly .graph_objs import graph_objs
7195
- d = scs . distance . pdist (X )
7196
- Z = sch . linkage ( d , method = 'complete' )
7203
+ d = distfun (X )
7204
+ Z = linkagefun ( d )
7197
7205
P = sch .dendrogram (Z , orientation = self .orientation ,
7198
7206
labels = self .labels , no_plot = True )
7199
7207
0 commit comments