From 77ae57b1f15328e27e93717b9e0fc03d49d1d615 Mon Sep 17 00:00:00 2001 From: Elpiro <32934904+Elpiro@users.noreply.github.com> Date: Fri, 27 Jul 2018 16:19:56 +0200 Subject: [PATCH 1/2] Added parameter to choose color_threshold --- plotly/figure_factory/_dendrogram.py | 29 ++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/plotly/figure_factory/_dendrogram.py b/plotly/figure_factory/_dendrogram.py index 72fc36c7d36..9628be81cf7 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'), - hovertext=None): + hovertext=None, color_threshold='default'): """ BETA function that returns a dendrogram Plotly figure object. @@ -30,6 +30,7 @@ def create_dendrogram(X, orientation="bottom", labels=None, :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 + :param (double) color_threshold: Value at which the separation of clusters will be made clusters @@ -88,20 +89,19 @@ def create_dendrogram(X, orientation="bottom", labels=None, dendrogram = _Dendrogram(X, orientation, labels, colorscale, distfun=distfun, linkagefun=linkagefun, - hovertext=hovertext) + hovertext=hovertext, color_threshold=color_threshold) - return graph_objs.Figure(data=dendrogram.data, - layout=dendrogram.layout) + return graph_objs.Figure(data=dendrogram.data, layout=dendrogram.layout) class _Dendrogram(object): """Refer to FigureFactory.create_dendrogram() for docstring.""" def __init__(self, X, orientation='bottom', labels=None, colorscale=None, - width=np.inf, height=np.inf, xaxis='xaxis', yaxis='yaxis', + width="100%", height="100%", xaxis='xaxis', yaxis='yaxis', distfun=None, linkagefun=lambda x: sch.linkage(x, 'complete'), - hovertext=None): + hovertext=None, color_threshold='default'): self.orientation = orientation self.labels = labels self.xaxis = xaxis @@ -127,8 +127,9 @@ 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, - hovertext) + linkagefun, + hovertext, + color_threshold) self.labels = ordered_labels self.leaves = leaves @@ -144,7 +145,7 @@ def __init__(self, X, orientation='bottom', labels=None, colorscale=None, self.zero_vals.sort() self.layout = self.set_figure_layout(width, height) - self.data = dd_traces + self.data = graph_objs.Data(dd_traces) def get_color_dict(self, colorscale): """ @@ -237,7 +238,7 @@ def set_figure_layout(self, width, height): return self.layout - def get_dendrogram_traces(self, X, colorscale, distfun, linkagefun, hovertext): + def get_dendrogram_traces(self, X, colorscale, distfun, linkagefun, hovertext, color_threshold): """ Calculates all the elements needed for plotting a dendrogram. @@ -262,7 +263,8 @@ def get_dendrogram_traces(self, X, colorscale, distfun, linkagefun, hovertext): d = distfun(X) Z = linkagefun(d) P = sch.dendrogram(Z, orientation=self.orientation, - labels=self.labels, no_plot=True) + labels=self.labels, no_plot=True, + color_threshold = color_threshold) icoord = scp.array(P['icoord']) dcoord = scp.array(P['dcoord']) @@ -288,12 +290,11 @@ def get_dendrogram_traces(self, X, colorscale, distfun, linkagefun, hovertext): hovertext_label = None if hovertext: hovertext_label = hovertext[i] - trace = dict( - type='scatter', + trace = graph_objs.Scatter( x=np.multiply(self.sign[self.xaxis], xs), y=np.multiply(self.sign[self.yaxis], ys), mode='lines', - marker=dict(color=colors[color_key]), + marker=graph_objs.Marker(color=colors[color_key]), text=hovertext_label, hoverinfo='text' ) From 6e7ae79d57d045d9a4744c04b9796853d168348c Mon Sep 17 00:00:00 2001 From: Elpiro <32934904+Elpiro@users.noreply.github.com> Date: Wed, 19 Sep 2018 14:27:25 +0200 Subject: [PATCH 2/2] Update _dendrogram.py --- plotly/figure_factory/_dendrogram.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/plotly/figure_factory/_dendrogram.py b/plotly/figure_factory/_dendrogram.py index 9628be81cf7..f5418c3b2bc 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'), - hovertext=None, color_threshold='default'): + hovertext=None, color_threshold=None): """ BETA function that returns a dendrogram Plotly figure object. @@ -30,8 +30,8 @@ def create_dendrogram(X, orientation="bottom", labels=None, :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 - :param (double) color_threshold: Value at which the separation of clusters will be made - + :param (double) color_threshold: Value at which the separation of clusters will be made + clusters Example 1: Simple bottom oriented dendrogram @@ -91,17 +91,18 @@ def create_dendrogram(X, orientation="bottom", labels=None, distfun=distfun, linkagefun=linkagefun, hovertext=hovertext, color_threshold=color_threshold) - return graph_objs.Figure(data=dendrogram.data, layout=dendrogram.layout) + return graph_objs.Figure(data=dendrogram.data, + layout=dendrogram.layout) class _Dendrogram(object): """Refer to FigureFactory.create_dendrogram() for docstring.""" def __init__(self, X, orientation='bottom', labels=None, colorscale=None, - width="100%", height="100%", xaxis='xaxis', yaxis='yaxis', + width=np.inf, height=np.inf, xaxis='xaxis', yaxis='yaxis', distfun=None, linkagefun=lambda x: sch.linkage(x, 'complete'), - hovertext=None, color_threshold='default'): + hovertext=None, color_threshold=None): self.orientation = orientation self.labels = labels self.xaxis = xaxis @@ -127,9 +128,9 @@ 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, - color_threshold) + color_threshold) self.labels = ordered_labels self.leaves = leaves @@ -145,7 +146,7 @@ def __init__(self, X, orientation='bottom', labels=None, colorscale=None, self.zero_vals.sort() self.layout = self.set_figure_layout(width, height) - self.data = graph_objs.Data(dd_traces) + self.data = dd_traces def get_color_dict(self, colorscale): """ @@ -264,7 +265,7 @@ def get_dendrogram_traces(self, X, colorscale, distfun, linkagefun, hovertext, c Z = linkagefun(d) P = sch.dendrogram(Z, orientation=self.orientation, labels=self.labels, no_plot=True, - color_threshold = color_threshold) + color_threshold=color_threshold) icoord = scp.array(P['icoord']) dcoord = scp.array(P['dcoord']) @@ -290,11 +291,12 @@ def get_dendrogram_traces(self, X, colorscale, distfun, linkagefun, hovertext, c hovertext_label = None if hovertext: hovertext_label = hovertext[i] - trace = graph_objs.Scatter( + trace = dict( + type='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=dict(color=colors[color_key]), text=hovertext_label, hoverinfo='text' )