@@ -2304,37 +2304,31 @@ def create_dendrogram(X, orientation="bottom", labels=None,
2304
2304
:param (list) colorscale: Optional colorscale for dendrogram tree
2305
2305
clusters
2306
2306
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
-
2313
2307
Example 1: Simple bottom oriented dendrogram
2314
2308
```
2309
+ import numpy as np
2310
+
2315
2311
import plotly.plotly as py
2316
2312
from plotly.tools import FigureFactory as FF
2317
2313
2318
- import numpy as np
2319
-
2320
2314
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)
2323
2317
2324
2318
```
2325
2319
2326
2320
Example 2: Dendrogram to put on the left of the heatmap
2327
2321
```
2328
2322
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 )
2331
2325
2332
- py.iplot(dendro_X , validate=False, height=1000, width=300)
2326
+ py.iplot(dendro , validate=False, height=1000, width=300)
2333
2327
```
2334
2328
"""
2335
2329
dependencies = (_scipy_imported and _scipy__spatial_imported and
2336
2330
_scipy__cluster__hierarchy_imported )
2337
-
2331
+
2338
2332
if dependencies is False :
2339
2333
raise ImportError ("FigureFactory.create_dendrogram requires scipy, \
2340
2334
scipy.spatial and scipy.hierarchy" )
@@ -2946,7 +2940,7 @@ def get_candle_decrease(self):
2946
2940
2947
2941
2948
2942
class _Dendrogram (FigureFactory ):
2949
-
2943
+
2950
2944
"""
2951
2945
Refer to FigureFactory.create_dendrogram() for docstring.
2952
2946
"""
@@ -2971,10 +2965,10 @@ def __init__(self, X, orientation='bottom', labels=None, colorscale=None,
2971
2965
self .sign [self .yaxis ] = 1
2972
2966
else :
2973
2967
self .sign [self .yaxis ] = - 1
2974
-
2968
+
2975
2969
(dd_traces , xvals , yvals ,
2976
2970
ordered_labels , leaves ) = self .get_dendrogram_traces (X , colorscale )
2977
-
2971
+
2978
2972
self .labels = ordered_labels
2979
2973
self .leaves = leaves
2980
2974
yvals_flat = yvals .flatten ()
@@ -2987,17 +2981,19 @@ def __init__(self, X, orientation='bottom', labels=None, colorscale=None,
2987
2981
self .zero_vals .append (xvals_flat [i ])
2988
2982
2989
2983
self .zero_vals .sort ()
2990
-
2984
+
2991
2985
self .layout = self .set_figure_layout (width , height )
2992
2986
self .data = Data (dd_traces )
2993
2987
2994
2988
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
2999
2989
"""
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
+
3001
2997
# These are the color codes returned for dendrograms
3002
2998
# We're replacing them with nicer colors
3003
2999
d = {'r' : 'red' ,
@@ -3025,75 +3021,79 @@ def get_color_dict(self, colorscale):
3025
3021
k = default_colors .keys ()[i ]
3026
3022
if i < len (colorscale ):
3027
3023
default_colors [k ] = colorscale [i ]
3028
-
3024
+
3029
3025
return default_colors
3030
-
3026
+
3031
3027
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
3035
3032
"""
3036
3033
3037
3034
axis_defaults = {
3038
3035
'type' : 'linear' ,
3039
3036
'ticks' : 'outside' ,
3040
3037
'mirror' : 'allticks' ,
3041
3038
'rangemode' : 'tozero' ,
3042
- 'showticklabels' : True ,
3039
+ 'showticklabels' : True ,
3043
3040
'zeroline' : False ,
3044
- 'showgrid' : False ,
3041
+ 'showgrid' : False ,
3045
3042
'showline' : True ,
3046
3043
}
3047
-
3044
+
3048
3045
if len (self .labels ) != 0 :
3049
3046
axis_key_labels = self .xaxis
3050
3047
if self .orientation in ['left' , 'right' ]:
3051
3048
axis_key_labels = self .yaxis
3052
3049
if axis_key_labels not in self .layout :
3053
3050
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 ]
3056
3053
self .layout [axis_key_labels ]['ticktext' ] = self .labels
3057
3054
self .layout [axis_key_labels ]['tickmode' ] = 'array'
3058
-
3055
+
3059
3056
self .layout [axis_key ].update (axis_defaults )
3060
-
3057
+
3061
3058
return self .layout [axis_key ]
3062
3059
3063
3060
def set_figure_layout (self , width , height ):
3064
- """
3065
- Sets and returns default layout object for dendrogram figure
3066
3061
"""
3067
-
3062
+ Sets and returns default layout object for dendrogram figure
3063
+ """
3064
+
3068
3065
self .layout .update ({
3069
3066
'showlegend' : False ,
3070
- 'autoscale' : False ,
3067
+ 'autoscale' : False ,
3071
3068
'hovermode' : 'closest' ,
3072
3069
'width' : width ,
3073
3070
'width' : height
3074
3071
})
3075
-
3072
+
3076
3073
self .set_axis_layout (self .xaxis )
3077
3074
self .set_axis_layout (self .yaxis )
3078
3075
3079
3076
return self .layout
3080
3077
3081
3078
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
+
3097
3097
d = scs .distance .pdist (X )
3098
3098
Z = sch .linkage (d , method = 'complete' )
3099
3099
P = sch .dendrogram (Z , orientation = self .orientation ,
@@ -3104,9 +3104,9 @@ def get_dendrogram_traces(self, X, colorscale):
3104
3104
ordered_labels = scp .array (P ['ivl' ])
3105
3105
color_list = scp .array (P ['color_list' ])
3106
3106
colors = self .get_color_dict (colorscale )
3107
-
3107
+
3108
3108
trace_list = []
3109
-
3109
+
3110
3110
for i in range (len (icoord )):
3111
3111
# xs and ys are arrays of 4 points that make up the '∩' shapes
3112
3112
# of the dendrogram tree
@@ -3139,6 +3139,5 @@ def get_dendrogram_traces(self, X, colorscale):
3139
3139
trace ['yaxis' ] = 'y' + y_index
3140
3140
3141
3141
trace_list .append (trace )
3142
-
3143
- return trace_list , icoord , dcoord , ordered_labels , P ['leaves' ]
3144
3142
3143
+ return trace_list , icoord , dcoord , ordered_labels , P ['leaves' ]
0 commit comments