Skip to content

Commit 88842b1

Browse files
committed
cow comments
1 parent 20fc40c commit 88842b1

File tree

1 file changed

+58
-59
lines changed

1 file changed

+58
-59
lines changed

plotly/tools.py

Lines changed: 58 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2304,37 +2304,31 @@ def create_dendrogram(X, orientation="bottom", labels=None,
23042304
:param (list) colorscale: Optional colorscale for dendrogram tree
23052305
clusters
23062306
2307-
X: Heatmap matrix as array of arrays
2308-
orientation: 'top', 'right', 'bottom', or 'left'
2309-
labels: List of axis category labels
2310-
colorscale: Optional colorscale for dendrogram tree clusters
2311-
2312-
23132307
Example 1: Simple bottom oriented dendrogram
23142308
```
2309+
import numpy as np
2310+
23152311
import plotly.plotly as py
23162312
from plotly.tools import FigureFactory as FF
23172313
2318-
import numpy as np
2319-
23202314
X = np.random.rand(5,5)
2321-
dendro_X = FF.create_dendrogram(X)
2322-
py.iplot(dendro_X, validate=False, height=300, width=1000)
2315+
dendro = FF.create_dendrogram(X)
2316+
py.iplot(dendro, validate=False, height=300, width=1000)
23232317
23242318
```
23252319
23262320
Example 2: Dendrogram to put on the left of the heatmap
23272321
```
23282322
X = np.random.rand(5,5)
2329-
names_X = ['Jack', 'Oxana', 'John', 'Chelsea', 'Mark']
2330-
dendro_X = FF.create_dendrogram(X, orientation='right', labels=names_X)
2323+
names = ['Jack', 'Oxana', 'John', 'Chelsea', 'Mark']
2324+
dendro = FF.create_dendrogram(X, orientation='right', labels=names)
23312325
2332-
py.iplot(dendro_X, validate=False, height=1000, width=300)
2326+
py.iplot(dendro, validate=False, height=1000, width=300)
23332327
```
23342328
"""
23352329
dependencies = (_scipy_imported and _scipy__spatial_imported and
23362330
_scipy__cluster__hierarchy_imported)
2337-
2331+
23382332
if dependencies is False:
23392333
raise ImportError("FigureFactory.create_dendrogram requires scipy, \
23402334
scipy.spatial and scipy.hierarchy")
@@ -2946,7 +2940,7 @@ def get_candle_decrease(self):
29462940

29472941

29482942
class _Dendrogram(FigureFactory):
2949-
2943+
29502944
"""
29512945
Refer to FigureFactory.create_dendrogram() for docstring.
29522946
"""
@@ -2971,10 +2965,10 @@ def __init__(self, X, orientation='bottom', labels=None, colorscale=None,
29712965
self.sign[self.yaxis] = 1
29722966
else:
29732967
self.sign[self.yaxis] = -1
2974-
2968+
29752969
(dd_traces, xvals, yvals,
29762970
ordered_labels, leaves) = self.get_dendrogram_traces(X, colorscale)
2977-
2971+
29782972
self.labels = ordered_labels
29792973
self.leaves = leaves
29802974
yvals_flat = yvals.flatten()
@@ -2987,17 +2981,19 @@ def __init__(self, X, orientation='bottom', labels=None, colorscale=None,
29872981
self.zero_vals.append(xvals_flat[i])
29882982

29892983
self.zero_vals.sort()
2990-
2984+
29912985
self.layout = self.set_figure_layout(width, height)
29922986
self.data = Data(dd_traces)
29932987

29942988
def get_color_dict(self, colorscale):
2995-
"""
2996-
Returns colorscale used for dendrogram tree clusters
2997-
:param (list) colorscale: colors to use for the plot,
2998-
in rgb format
29992989
"""
3000-
2990+
Returns colorscale used for dendrogram tree clusters
2991+
:param (list) colorscale: colors to use for the plot,
2992+
in rgb format
2993+
:rtype (dict): returns a dictionary of default colors mapped
2994+
to the user colorscale
2995+
"""
2996+
30012997
# These are the color codes returned for dendrograms
30022998
# We're replacing them with nicer colors
30032999
d = {'r': 'red',
@@ -3025,75 +3021,79 @@ def get_color_dict(self, colorscale):
30253021
k = default_colors.keys()[i]
30263022
if i < len(colorscale):
30273023
default_colors[k] = colorscale[i]
3028-
3024+
30293025
return default_colors
3030-
3026+
30313027
def set_axis_layout(self, axis_key):
3032-
"""
3033-
Sets and returns default axis object for dendrogram figure
3034-
:param (str) axis_key: "xaxis", "xaxis1", "yaxis", yaxis1", etc.
3028+
"""
3029+
Sets and returns default axis object for dendrogram figure
3030+
:param (str) axis_key: "xaxis", "xaxis1", "yaxis", yaxis1", etc.
3031+
:rtype (dict): returns an axis_key dictionary with set parameters
30353032
"""
30363033

30373034
axis_defaults = {
30383035
'type': 'linear',
30393036
'ticks': 'outside',
30403037
'mirror': 'allticks',
30413038
'rangemode': 'tozero',
3042-
'showticklabels': True,
3039+
'showticklabels': True,
30433040
'zeroline': False,
3044-
'showgrid': False,
3041+
'showgrid': False,
30453042
'showline': True,
30463043
}
3047-
3044+
30483045
if len(self.labels) != 0:
30493046
axis_key_labels = self.xaxis
30503047
if self.orientation in ['left', 'right']:
30513048
axis_key_labels = self.yaxis
30523049
if axis_key_labels not in self.layout:
30533050
self.layout[axis_key_labels] = {}
3054-
self.layout[axis_key_labels]['tickvals'] = [ea*self.sign[axis_key]
3055-
for ea in self.zero_vals]
3051+
self.layout[axis_key_labels]['tickvals'] = [zv*self.sign[axis_key]
3052+
for zv in self.zero_vals]
30563053
self.layout[axis_key_labels]['ticktext'] = self.labels
30573054
self.layout[axis_key_labels]['tickmode'] = 'array'
3058-
3055+
30593056
self.layout[axis_key].update(axis_defaults)
3060-
3057+
30613058
return self.layout[axis_key]
30623059

30633060
def set_figure_layout(self, width, height):
3064-
"""
3065-
Sets and returns default layout object for dendrogram figure
30663061
"""
3067-
3062+
Sets and returns default layout object for dendrogram figure
3063+
"""
3064+
30683065
self.layout.update({
30693066
'showlegend': False,
3070-
'autoscale': False,
3067+
'autoscale': False,
30713068
'hovermode': 'closest',
30723069
'width': width,
30733070
'width': height
30743071
})
3075-
3072+
30763073
self.set_axis_layout(self.xaxis)
30773074
self.set_axis_layout(self.yaxis)
30783075

30793076
return self.layout
30803077

30813078
def get_dendrogram_traces(self, X, colorscale):
3082-
"""
3083-
Calculates all the elements needed for plotting a dendrogram
3084-
3085-
:rtype (tuple): Contains all the traces in the following order
3086-
(a) trace_list: List of Plotly trace objects for the dendrogram
3087-
tree
3088-
(b) icoord: All X points of the dendogram tree as array of arrays
3089-
with length 4
3090-
(c) dcoord: All Y points of the dendogram tree as array of arrays
3091-
with length 4
3092-
(d) ordered_labels: leaf labels in the order they are going to
3093-
appear on the plot
3094-
(e) P['leaves']: left-to-right traversal of the leaves
3095-
"""
3096-
3079+
"""
3080+
Calculates all the elements needed for plotting a dendrogram
3081+
3082+
:param (ndarray) X: Matrix of observations as arrray of arrays
3083+
:param (list) colorscale: Optional colorscale for dendrogram tree
3084+
clusters
3085+
3086+
:rtype (tuple): Contains all the traces in the following order
3087+
(a) trace_list: List of Plotly trace objects for the dendrogram tree
3088+
(b) icoord: All X points of the dendogram tree as array of arrays
3089+
with length 4
3090+
(c) dcoord: All Y points of the dendogram tree as array of arrays
3091+
with length 4
3092+
(d) ordered_labels: leaf labels in the order they are going to
3093+
appear on the plot
3094+
(e) P['leaves']: left-to-right traversal of the leaves
3095+
"""
3096+
30973097
d = scs.distance.pdist(X)
30983098
Z = sch.linkage(d, method='complete')
30993099
P = sch.dendrogram(Z, orientation=self.orientation,
@@ -3104,9 +3104,9 @@ def get_dendrogram_traces(self, X, colorscale):
31043104
ordered_labels = scp.array(P['ivl'])
31053105
color_list = scp.array(P['color_list'])
31063106
colors = self.get_color_dict(colorscale)
3107-
3107+
31083108
trace_list = []
3109-
3109+
31103110
for i in range(len(icoord)):
31113111
# xs and ys are arrays of 4 points that make up the '∩' shapes
31123112
# of the dendrogram tree
@@ -3139,6 +3139,5 @@ def get_dendrogram_traces(self, X, colorscale):
31393139
trace['yaxis'] = 'y' + y_index
31403140

31413141
trace_list.append(trace)
3142-
3143-
return trace_list, icoord, dcoord, ordered_labels, P['leaves']
31443142

3143+
return trace_list, icoord, dcoord, ordered_labels, P['leaves']

0 commit comments

Comments
 (0)