Skip to content

Add annotation parameter for dendrogram #807

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

Merged
merged 2 commits into from
Aug 31, 2017
Merged
Changes from all commits
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
23 changes: 17 additions & 6 deletions plotly/figure_factory/_dendrogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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

Expand Down Expand Up @@ -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}
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.

Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down