16
16
17
17
def create_dendrogram (X , orientation = "bottom" , labels = None ,
18
18
colorscale = None , distfun = None ,
19
- linkagefun = lambda x : sch .linkage (x , 'complete' )):
19
+ linkagefun = lambda x : sch .linkage (x , 'complete' ),
20
+ hovertext = None ):
20
21
"""
21
22
BETA function that returns a dendrogram Plotly figure object.
22
23
@@ -28,6 +29,7 @@ def create_dendrogram(X, orientation="bottom", labels=None,
28
29
the observations
29
30
:param (function) linkagefun: Function to compute the linkage matrix from
30
31
the pairwise distances
32
+ :param (list[list]) hovertext: List of hovertext for constituent traces of dendrogram
31
33
32
34
clusters
33
35
@@ -85,7 +87,8 @@ def create_dendrogram(X, orientation="bottom", labels=None,
85
87
distfun = scs .distance .pdist
86
88
87
89
dendrogram = _Dendrogram (X , orientation , labels , colorscale ,
88
- distfun = distfun , linkagefun = linkagefun )
90
+ distfun = distfun , linkagefun = linkagefun ,
91
+ hovertext = hovertext )
89
92
90
93
return graph_objs .Figure (data = dendrogram .data , layout = dendrogram .layout )
91
94
@@ -96,7 +99,8 @@ class _Dendrogram(object):
96
99
def __init__ (self , X , orientation = 'bottom' , labels = None , colorscale = None ,
97
100
width = "100%" , height = "100%" , xaxis = 'xaxis' , yaxis = 'yaxis' ,
98
101
distfun = None ,
99
- linkagefun = lambda x : sch .linkage (x , 'complete' )):
102
+ linkagefun = lambda x : sch .linkage (x , 'complete' ),
103
+ hovertext = None ):
100
104
self .orientation = orientation
101
105
self .labels = labels
102
106
self .xaxis = xaxis
@@ -122,7 +126,8 @@ def __init__(self, X, orientation='bottom', labels=None, colorscale=None,
122
126
(dd_traces , xvals , yvals ,
123
127
ordered_labels , leaves ) = self .get_dendrogram_traces (X , colorscale ,
124
128
distfun ,
125
- linkagefun )
129
+ linkagefun ,
130
+ hovertext )
126
131
127
132
self .labels = ordered_labels
128
133
self .leaves = leaves
@@ -231,7 +236,7 @@ def set_figure_layout(self, width, height):
231
236
232
237
return self .layout
233
238
234
- def get_dendrogram_traces (self , X , colorscale , distfun , linkagefun ):
239
+ def get_dendrogram_traces (self , X , colorscale , distfun , linkagefun , hovertext ):
235
240
"""
236
241
Calculates all the elements needed for plotting a dendrogram.
237
242
@@ -241,6 +246,7 @@ def get_dendrogram_traces(self, X, colorscale, distfun, linkagefun):
241
246
from the observations
242
247
:param (function) linkagefun: Function to compute the linkage matrix
243
248
from the pairwise distances
249
+ :param (list) hovertext: List of hovertext for constituent traces of dendrogram
244
250
:rtype (tuple): Contains all the traces in the following order:
245
251
(a) trace_list: List of Plotly trace objects for dendrogram tree
246
252
(b) icoord: All X points of the dendrogram tree as array of arrays
@@ -278,11 +284,16 @@ def get_dendrogram_traces(self, X, colorscale, distfun, linkagefun):
278
284
else :
279
285
ys = icoord [i ]
280
286
color_key = color_list [i ]
287
+ hovertext_label = None
288
+ if hovertext :
289
+ hovertext_label = hovertext [i ]
281
290
trace = graph_objs .Scatter (
282
291
x = np .multiply (self .sign [self .xaxis ], xs ),
283
292
y = np .multiply (self .sign [self .yaxis ], ys ),
284
293
mode = 'lines' ,
285
- marker = graph_objs .Marker (color = colors [color_key ])
294
+ marker = graph_objs .Marker (color = colors [color_key ]),
295
+ text = hovertext_label ,
296
+ hoverinfo = 'text'
286
297
)
287
298
288
299
try :
0 commit comments