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