Skip to content

Commit 75673a9

Browse files
authored
Merge pull request #807 from parulsethi/dendrogram_annotation
Add annotation parameter for dendrogram
2 parents 2e0bc3f + d37679e commit 75673a9

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

Diff for: plotly/figure_factory/_dendrogram.py

+17-6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
def create_dendrogram(X, orientation="bottom", labels=None,
1818
colorscale=None, distfun=None,
19-
linkagefun=lambda x: sch.linkage(x, 'complete')):
19+
linkagefun=lambda x: sch.linkage(x, 'complete'),
20+
hovertext=None):
2021
"""
2122
BETA function that returns a dendrogram Plotly figure object.
2223
@@ -28,6 +29,7 @@ def create_dendrogram(X, orientation="bottom", labels=None,
2829
the observations
2930
:param (function) linkagefun: Function to compute the linkage matrix from
3031
the pairwise distances
32+
:param (list[list]) hovertext: List of hovertext for constituent traces of dendrogram
3133
3234
clusters
3335
@@ -85,7 +87,8 @@ def create_dendrogram(X, orientation="bottom", labels=None,
8587
distfun = scs.distance.pdist
8688

8789
dendrogram = _Dendrogram(X, orientation, labels, colorscale,
88-
distfun=distfun, linkagefun=linkagefun)
90+
distfun=distfun, linkagefun=linkagefun,
91+
hovertext=hovertext)
8992

9093
return graph_objs.Figure(data=dendrogram.data, layout=dendrogram.layout)
9194

@@ -96,7 +99,8 @@ class _Dendrogram(object):
9699
def __init__(self, X, orientation='bottom', labels=None, colorscale=None,
97100
width="100%", height="100%", xaxis='xaxis', yaxis='yaxis',
98101
distfun=None,
99-
linkagefun=lambda x: sch.linkage(x, 'complete')):
102+
linkagefun=lambda x: sch.linkage(x, 'complete'),
103+
hovertext=None):
100104
self.orientation = orientation
101105
self.labels = labels
102106
self.xaxis = xaxis
@@ -122,7 +126,8 @@ def __init__(self, X, orientation='bottom', labels=None, colorscale=None,
122126
(dd_traces, xvals, yvals,
123127
ordered_labels, leaves) = self.get_dendrogram_traces(X, colorscale,
124128
distfun,
125-
linkagefun)
129+
linkagefun,
130+
hovertext)
126131

127132
self.labels = ordered_labels
128133
self.leaves = leaves
@@ -231,7 +236,7 @@ def set_figure_layout(self, width, height):
231236

232237
return self.layout
233238

234-
def get_dendrogram_traces(self, X, colorscale, distfun, linkagefun):
239+
def get_dendrogram_traces(self, X, colorscale, distfun, linkagefun, hovertext):
235240
"""
236241
Calculates all the elements needed for plotting a dendrogram.
237242
@@ -241,6 +246,7 @@ def get_dendrogram_traces(self, X, colorscale, distfun, linkagefun):
241246
from the observations
242247
:param (function) linkagefun: Function to compute the linkage matrix
243248
from the pairwise distances
249+
:param (list) hovertext: List of hovertext for constituent traces of dendrogram
244250
:rtype (tuple): Contains all the traces in the following order:
245251
(a) trace_list: List of Plotly trace objects for dendrogram tree
246252
(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):
278284
else:
279285
ys = icoord[i]
280286
color_key = color_list[i]
287+
hovertext_label = None
288+
if hovertext:
289+
hovertext_label = hovertext[i]
281290
trace = graph_objs.Scatter(
282291
x=np.multiply(self.sign[self.xaxis], xs),
283292
y=np.multiply(self.sign[self.yaxis], ys),
284293
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'
286297
)
287298

288299
try:

0 commit comments

Comments
 (0)