diff --git a/plotly/figure_factory/_dendrogram.py b/plotly/figure_factory/_dendrogram.py index 26e6692290d..8e956d26d64 100644 --- a/plotly/figure_factory/_dendrogram.py +++ b/plotly/figure_factory/_dendrogram.py @@ -16,7 +16,8 @@ def create_dendrogram(X, orientation="bottom", labels=None, colorscale=None, distfun=None, - linkagefun=lambda x: sch.linkage(x, 'complete')): + linkagefun=lambda x: sch.linkage(x, 'complete'), + hovertext=None): """ BETA function that returns a dendrogram Plotly figure object. @@ -28,6 +29,7 @@ def create_dendrogram(X, orientation="bottom", labels=None, the observations :param (function) linkagefun: Function to compute the linkage matrix from the pairwise distances + :param (list[list]) hovertext: List of hovertext for constituent traces of dendrogram clusters @@ -85,7 +87,8 @@ def create_dendrogram(X, orientation="bottom", labels=None, distfun = scs.distance.pdist dendrogram = _Dendrogram(X, orientation, labels, colorscale, - distfun=distfun, linkagefun=linkagefun) + distfun=distfun, linkagefun=linkagefun, + hovertext=hovertext) return {'layout': dendrogram.layout, 'data': dendrogram.data} @@ -97,7 +100,8 @@ class _Dendrogram(object): def __init__(self, X, orientation='bottom', labels=None, colorscale=None, width="100%", height="100%", xaxis='xaxis', yaxis='yaxis', distfun=None, - linkagefun=lambda x: sch.linkage(x, 'complete')): + linkagefun=lambda x: sch.linkage(x, 'complete'), + hovertext=None): self.orientation = orientation self.labels = labels self.xaxis = xaxis @@ -123,7 +127,8 @@ def __init__(self, X, orientation='bottom', labels=None, colorscale=None, (dd_traces, xvals, yvals, ordered_labels, leaves) = self.get_dendrogram_traces(X, colorscale, distfun, - linkagefun) + linkagefun, + hovertext) self.labels = ordered_labels self.leaves = leaves @@ -232,7 +237,7 @@ def set_figure_layout(self, width, height): return self.layout - def get_dendrogram_traces(self, X, colorscale, distfun, linkagefun): + def get_dendrogram_traces(self, X, colorscale, distfun, linkagefun, hovertext): """ Calculates all the elements needed for plotting a dendrogram. @@ -242,6 +247,7 @@ def get_dendrogram_traces(self, X, colorscale, distfun, linkagefun): from the observations :param (function) linkagefun: Function to compute the linkage matrix from the pairwise distances + :param (list) hovertext: List of hovertext for constituent traces of dendrogram :rtype (tuple): Contains all the traces in the following order: (a) trace_list: List of Plotly trace objects for dendrogram tree (b) icoord: All X points of the dendrogram tree as array of arrays @@ -279,11 +285,16 @@ def get_dendrogram_traces(self, X, colorscale, distfun, linkagefun): else: ys = icoord[i] color_key = color_list[i] + hovertext_label = None + if hovertext: + hovertext_label = hovertext[i] trace = graph_objs.Scatter( x=np.multiply(self.sign[self.xaxis], xs), y=np.multiply(self.sign[self.yaxis], ys), mode='lines', - marker=graph_objs.Marker(color=colors[color_key]) + marker=graph_objs.Marker(color=colors[color_key]), + text=hovertext_label, + hoverinfo='text' ) try: