Skip to content

Commit 5cd888e

Browse files
committed
Added color_threshold to dendrogram
1 parent 0bde4e1 commit 5cd888e

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

plotly/figure_factory/_dendrogram.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
def create_dendrogram(X, orientation="bottom", labels=None,
1818
colorscale=None, distfun=None,
1919
linkagefun=lambda x: sch.linkage(x, 'complete'),
20-
hovertext=None):
20+
hovertext=None, color_threshold=None):
2121
"""
2222
BETA function that returns a dendrogram Plotly figure object.
2323
@@ -32,6 +32,7 @@ def create_dendrogram(X, orientation="bottom", labels=None,
3232
:param (list[list]) hovertext: List of hovertext for constituent traces of dendrogram
3333
3434
clusters
35+
:param (function) color_threshold: function to colour clusters under a set threshold
3536
3637
Example 1: Simple bottom oriented dendrogram
3738
```
@@ -88,7 +89,7 @@ def create_dendrogram(X, orientation="bottom", labels=None,
8889

8990
dendrogram = _Dendrogram(X, orientation, labels, colorscale,
9091
distfun=distfun, linkagefun=linkagefun,
91-
hovertext=hovertext)
92+
hovertext=hovertext, color_threshold=color_threshold)
9293

9394
return graph_objs.Figure(data=dendrogram.data, layout=dendrogram.layout)
9495

@@ -100,7 +101,7 @@ def __init__(self, X, orientation='bottom', labels=None, colorscale=None,
100101
width="100%", height="100%", xaxis='xaxis', yaxis='yaxis',
101102
distfun=None,
102103
linkagefun=lambda x: sch.linkage(x, 'complete'),
103-
hovertext=None):
104+
hovertext=None, color_threshold=None):
104105
self.orientation = orientation
105106
self.labels = labels
106107
self.xaxis = xaxis
@@ -127,7 +128,8 @@ def __init__(self, X, orientation='bottom', labels=None, colorscale=None,
127128
ordered_labels, leaves) = self.get_dendrogram_traces(X, colorscale,
128129
distfun,
129130
linkagefun,
130-
hovertext)
131+
hovertext,
132+
color_threshold)
131133

132134
self.labels = ordered_labels
133135
self.leaves = leaves
@@ -236,7 +238,7 @@ def set_figure_layout(self, width, height):
236238

237239
return self.layout
238240

239-
def get_dendrogram_traces(self, X, colorscale, distfun, linkagefun, hovertext):
241+
def get_dendrogram_traces(self, X, colorscale, distfun, linkagefun, hovertext, color_threshold):
240242
"""
241243
Calculates all the elements needed for plotting a dendrogram.
242244
@@ -247,6 +249,7 @@ def get_dendrogram_traces(self, X, colorscale, distfun, linkagefun, hovertext):
247249
:param (function) linkagefun: Function to compute the linkage matrix
248250
from the pairwise distances
249251
:param (list) hovertext: List of hovertext for constituent traces of dendrogram
252+
:param (list) color_threshold: sets the color of cluster under set threshold
250253
:rtype (tuple): Contains all the traces in the following order:
251254
(a) trace_list: List of Plotly trace objects for dendrogram tree
252255
(b) icoord: All X points of the dendrogram tree as array of arrays
@@ -261,7 +264,7 @@ def get_dendrogram_traces(self, X, colorscale, distfun, linkagefun, hovertext):
261264
d = distfun(X)
262265
Z = linkagefun(d)
263266
P = sch.dendrogram(Z, orientation=self.orientation,
264-
labels=self.labels, no_plot=True)
267+
labels=self.labels, no_plot=True, color_threshold=color_threshold)
265268

266269
icoord = scp.array(P['icoord'])
267270
dcoord = scp.array(P['dcoord'])

0 commit comments

Comments
 (0)