Skip to content

Added parameter to choose color_threshold #1075

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions plotly/figure_factory/_dendrogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please align with :param above


clusters

Expand Down Expand Up @@ -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',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"100%" is not a valid height/width and it looks like this is causing your test failure.

Where you trying to change some behavior here? I'm not sure why the old value was inf. I think the default should probably be just None, which will let the rending approach (plot, iplot, or FigureWidget) decide how to size the figure.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"100%" isn't a valid for a width/height. This is what's causing your test failure. I think this should probably be None, which will let the rendering approaches (plot, iplot, or FigureWidget) decide what size to use.

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
Expand All @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is there no closing parenthesis here?


self.labels = ordered_labels
self.leaves = leaves
Expand All @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

graph_objs.Data is deprecated in version 3. Just stick with dd_traces as a list or tuple.


def get_color_dict(self, colorscale):
"""
Expand Down Expand Up @@ -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.

Expand All @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please align with labels= above


icoord = scp.array(P['icoord'])
dcoord = scp.array(P['dcoord'])
Expand All @@ -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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's stick with dict (See below)

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]),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stick with dict or use graph_objs.scatter.Marker. graph_objs.Marker is deprecated in version 3

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

graph_objs.Marker is deprecated. So this should either stay a dict, or be graph_objs.scatter.Marker. Given the dendrogram performance discussion in #1052, I think we should avoid using graph_objs here and stick with dicts. This would also be consistent with my goals described in #1079

text=hovertext_label,
hoverinfo='text'
)
Expand Down