From c7c27b68c774c527ddbe1d6f04bff26b4b6471f4 Mon Sep 17 00:00:00 2001 From: parulsethi Date: Wed, 2 Aug 2017 03:06:37 +0530 Subject: [PATCH 1/2] add annotation parameter --- plotly/figure_factory/_dendrogram.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/plotly/figure_factory/_dendrogram.py b/plotly/figure_factory/_dendrogram.py index 26e6692290d..dfc7ffb1d32 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'), + annotation=None): """ BETA function that returns a dendrogram Plotly figure object. @@ -85,7 +86,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, + annotation=annotation) return {'layout': dendrogram.layout, 'data': dendrogram.data} @@ -97,7 +99,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'), + annotation=None): self.orientation = orientation self.labels = labels self.xaxis = xaxis @@ -123,7 +126,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, + annotation) self.labels = ordered_labels self.leaves = leaves @@ -232,7 +236,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, annotation): """ Calculates all the elements needed for plotting a dendrogram. @@ -279,11 +283,16 @@ def get_dendrogram_traces(self, X, colorscale, distfun, linkagefun): else: ys = icoord[i] color_key = color_list[i] + text_annotation = None + if annotation: + text_annotation = annotation[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=text_annotation, + hoverinfo='text' ) try: From d37679e16530ae730ef60e71b668dfd9bba42229 Mon Sep 17 00:00:00 2001 From: parulsethi Date: Sat, 5 Aug 2017 21:14:15 +0530 Subject: [PATCH 2/2] rename annotation to hovertext --- plotly/figure_factory/_dendrogram.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/plotly/figure_factory/_dendrogram.py b/plotly/figure_factory/_dendrogram.py index dfc7ffb1d32..8e956d26d64 100644 --- a/plotly/figure_factory/_dendrogram.py +++ b/plotly/figure_factory/_dendrogram.py @@ -17,7 +17,7 @@ def create_dendrogram(X, orientation="bottom", labels=None, colorscale=None, distfun=None, linkagefun=lambda x: sch.linkage(x, 'complete'), - annotation=None): + hovertext=None): """ BETA function that returns a dendrogram Plotly figure object. @@ -29,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 @@ -87,7 +88,7 @@ def create_dendrogram(X, orientation="bottom", labels=None, dendrogram = _Dendrogram(X, orientation, labels, colorscale, distfun=distfun, linkagefun=linkagefun, - annotation=annotation) + hovertext=hovertext) return {'layout': dendrogram.layout, 'data': dendrogram.data} @@ -100,7 +101,7 @@ 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'), - annotation=None): + hovertext=None): self.orientation = orientation self.labels = labels self.xaxis = xaxis @@ -127,7 +128,7 @@ def __init__(self, X, orientation='bottom', labels=None, colorscale=None, ordered_labels, leaves) = self.get_dendrogram_traces(X, colorscale, distfun, linkagefun, - annotation) + hovertext) self.labels = ordered_labels self.leaves = leaves @@ -236,7 +237,7 @@ def set_figure_layout(self, width, height): return self.layout - def get_dendrogram_traces(self, X, colorscale, distfun, linkagefun, annotation): + def get_dendrogram_traces(self, X, colorscale, distfun, linkagefun, hovertext): """ Calculates all the elements needed for plotting a dendrogram. @@ -246,6 +247,7 @@ def get_dendrogram_traces(self, X, colorscale, distfun, linkagefun, annotation): 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 @@ -283,15 +285,15 @@ def get_dendrogram_traces(self, X, colorscale, distfun, linkagefun, annotation): else: ys = icoord[i] color_key = color_list[i] - text_annotation = None - if annotation: - text_annotation = annotation[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]), - text=text_annotation, + text=hovertext_label, hoverinfo='text' )