@@ -2285,9 +2285,15 @@ def create_candlestick(open, high, low, close,
2285
2285
2286
2286
@staticmethod
2287
2287
def create_dendrogram (X , orientation = "bottom" , labels = None ,
2288
- colorscale = None , ** kwargs ):
2288
+ colorscale = None ):
2289
2289
"""
2290
- Returns a dendrogram Plotly figure object.
2290
+ BETA function that returns a dendrogram Plotly figure object.
2291
+
2292
+ :param (ndarray) X: Matrix of observations as arrray of arrays
2293
+ :param (str) orientation: 'top', 'right', 'bottom', or 'left'
2294
+ :param (list) labels: List of axis category labels(observation labels)
2295
+ :param (list) colorscale: Optional colorscale for dendrogram tree
2296
+ clusters
2291
2297
2292
2298
X: Heatmap matrix as array of arrays
2293
2299
orientation: 'top', 'right', 'bottom', or 'left'
@@ -2302,7 +2308,7 @@ def create_dendrogram(X, orientation="bottom", labels=None,
2302
2308
2303
2309
import numpy as np
2304
2310
2305
- X = np.random.rand(5,5)
2311
+ X = np.random.rand(5,5)
2306
2312
dendro_X = FF.create_dendrogram(X)
2307
2313
py.iplot(dendro_X, validate=False, height=300, width=1000)
2308
2314
@@ -2319,15 +2325,19 @@ def create_dendrogram(X, orientation="bottom", labels=None,
2319
2325
"""
2320
2326
2321
2327
if _scipy_imported is False :
2322
- raise ImportError ("FigureFactory.create_dendrogram requires scipy, scipy.spatial and scipy.hierarchy" )
2328
+ raise ImportError ("FigureFactory .create_dendrogram requires scipy ,
2329
+ scipy .spatial and scipy .hierarchy " )
2323
2330
2324
2331
s = X .shape
2325
2332
if len (s ) != 2 :
2326
2333
exceptions .PlotlyError ("X should be 2-dimensional array." )
2327
2334
2328
2335
dendrogram = _Dendrogram (X , orientation , labels , colorscale )
2329
2336
2330
- return {'layout' : dendrogram .layout , 'data' : dendrogram .data , 'labels' : dendrogram .labels }
2337
+ return {'layout' : dendrogram .layout ,
2338
+ 'data' : dendrogram .data ,
2339
+ 'labels' : dendrogram .labels }
2340
+
2331
2341
2332
2342
class _Quiver (FigureFactory ):
2333
2343
"""
@@ -2925,34 +2935,34 @@ def get_candle_decrease(self):
2925
2935
stick_decrease_y , stick_decrease_x )
2926
2936
2927
2937
class _Dendrogram (FigureFactory ):
2928
- ''' Returns a Dendrogram figure object
2929
- Example usage:
2930
- D = Dendrogram( Z )
2931
- fig = { 'data':D.data, 'layout':D.layout }
2932
- py.iplot( fig, filename='Dendro', validate=False )'''
2933
2938
2939
+ """
2940
+ Refer to FigureFactory.create_dendrogram() for docstring.
2941
+ """
2942
+
2934
2943
def __init__ (self , X , orientation = 'bottom' , labels = None , colorscale = None , \
2935
2944
width = "100%" , height = "100%" , xaxis = 'xaxis' , yaxis = 'yaxis' ):
2936
- ''' Draw a 2d dendrogram tree
2937
- X: Heatmap matrix as array of arrays
2938
- orientation: 'top', 'right', 'bottom', or 'left'
2939
- labels: List of axis category labels
2940
- colorscale: Optional colorscale for dendrogram tree clusters
2941
- Returns a dendrogram Plotly figure object '''
2942
-
2943
2945
self .orientation = orientation
2944
2946
self .labels = labels
2945
2947
self .xaxis = xaxis
2946
2948
self .yaxis = yaxis
2947
2949
self .data = []
2948
2950
self .leaves = []
2949
- self .sign = { self .xaxis :1 , self .yaxis :1 }
2950
- self .layout = { self .xaxis :{}, self .yaxis :{} }
2951
-
2952
- self .sign [self .xaxis ] = 1 if self .orientation in ['left' ,'bottom' ] else - 1
2953
- self .sign [self .yaxis ] = 1 if self .orientation in ['right' ,'bottom' ] else - 1
2951
+ self .sign = {self .xaxis : 1 , self .yaxis : 1 }
2952
+ self .layout = {self .xaxis : {}, self .yaxis : {}}
2953
+
2954
+ if self .orientation in ['left' , 'bottom' ]:
2955
+ self .sign [self .xaxis ] = 1
2956
+ else :
2957
+ self .sign [self .xaxis ] = - 1
2958
+
2959
+ if self .orientation in ['right' , 'bottom' ]:
2960
+ self .sign [self .yaxis ] = 1
2961
+ else :
2962
+ self .sign [self .yaxis ] = - 1
2954
2963
2955
- dd_traces , xvals , yvals , ordered_labels , leaves = self .get_dendrogram_traces ( X , colorscale )
2964
+ dd_traces , xvals , yvals ,
2965
+ ordered_labels , leaves = self .get_dendrogram_traces (X , colorscale )
2956
2966
2957
2967
self .labels = ordered_labels
2958
2968
self .leaves = leaves
@@ -2971,7 +2981,11 @@ def __init__(self, X, orientation='bottom', labels=None, colorscale=None, \
2971
2981
self .data = Data (dd_traces )
2972
2982
2973
2983
def get_color_dict (self , colorscale ):
2974
- ''' Return colorscale used for dendrogram tree clusters '''
2984
+ """
2985
+ Returns colorscale used for dendrogram tree clusters
2986
+ :param (list) colorscale: colors to use for the plot,
2987
+ in rgb format
2988
+ """
2975
2989
2976
2990
# These are the color codes returned for dendrograms
2977
2991
# We're replacing them with nicer colors
@@ -3004,9 +3018,11 @@ def get_color_dict(self, colorscale):
3004
3018
return default_colors
3005
3019
3006
3020
def set_axis_layout (self , axis_key ):
3007
- ''' Sets and returns default axis object for dendrogram figure
3008
- axis_key: "xaxis", "xaxis1", "yaxis", yaxis1", etc '''
3009
-
3021
+ """
3022
+ Sets and returns default axis object for dendrogram figure
3023
+ :param (str) axis_key: "xaxis", "xaxis1", "yaxis", yaxis1", etc.
3024
+ """
3025
+
3010
3026
axis_defaults = {
3011
3027
'type' : 'linear' ,
3012
3028
'ticks' : 'outside' ,
@@ -3024,7 +3040,8 @@ def set_axis_layout(self, axis_key):
3024
3040
axis_key_labels = self .yaxis
3025
3041
if axis_key_labels not in self .layout :
3026
3042
self .layout [axis_key_labels ] = {}
3027
- self .layout [axis_key_labels ]['tickvals' ] = [ea * self .sign [axis_key ] for ea in self .zero_vals ]
3043
+ self .layout [axis_key_labels ]['tickvals' ] = [ea * self .sign [axis_key ]
3044
+ for ea in self .zero_vals ]
3028
3045
self .layout [axis_key_labels ]['ticktext' ] = self .labels
3029
3046
self .layout [axis_key_labels ]['tickmode' ] = 'array'
3030
3047
@@ -3033,11 +3050,13 @@ def set_axis_layout(self, axis_key):
3033
3050
return self .layout [axis_key ]
3034
3051
3035
3052
def set_figure_layout (self , width , height ):
3036
- ''' Sets and returns default layout object for dendrogram figure '''
3053
+ """
3054
+ Sets and returns default layout object for dendrogram figure
3055
+ """
3037
3056
3038
3057
self .layout .update ({
3039
3058
'showlegend' : False ,
3040
- 'autoscale' : False ,
3059
+ 'autoscale' : False ,
3041
3060
'hovermode' : 'closest' ,
3042
3061
'width' : width ,
3043
3062
'width' : height
@@ -3048,35 +3067,55 @@ def set_figure_layout(self, width, height):
3048
3067
3049
3068
return self .layout
3050
3069
3051
- def get_dendrogram_traces ( self , X , colorscale ):
3052
- ''' Returns a tuple with:
3053
- (a) List of Plotly trace objects for the dendrogram tree
3054
- (b) icoord: All X points of the dendogram tree as array of arrays with length 4
3055
- (c) dcoord: All Y points of the dendogram tree as array of arrays with length 4 '''
3070
+ def get_dendrogram_traces (self , X , colorscale ):
3071
+ """
3072
+ Calculates all the elements needed for plotting a dendrogram
3073
+
3074
+ :rtype (tuple): Contains all the traces in the following order
3075
+ (a) trace_list: List of Plotly trace objects for the dendrogram
3076
+ tree
3077
+ (b) icoord: All X points of the dendogram tree as array of arrays
3078
+ with length 4
3079
+ (c) dcoord: All Y points of the dendogram tree as array of arrays
3080
+ with length 4
3081
+ (d) ordered_labels: leaf labels in the order they are going to
3082
+ appear on the plot
3083
+ (e) P['leaves']: left-to-right traversal of the leaves
3084
+ """
3056
3085
3057
3086
d = scs .distance .pdist (X )
3058
- Z = sch .linkage (d , method = 'complete' )
3059
- P = sch .dendrogram (Z , orientation = self .orientation ,labels = self .labels , no_plot = True )
3060
-
3061
- icoord = scp .array ( P ['icoord' ] )
3062
- dcoord = scp .array ( P ['dcoord' ] )
3063
- ordered_labels = scp .array ( P ['ivl' ] )
3064
- color_list = scp .array ( P ['color_list' ] )
3065
- colors = self .get_color_dict ( colorscale )
3087
+ Z = sch .linkage (d , method = 'complete' )
3088
+ P = sch .dendrogram (Z , orientation = self .orientation ,
3089
+ labels = self .labels , no_plot = True )
3090
+
3091
+ icoord = scp .array (P ['icoord' ])
3092
+ dcoord = scp .array (P ['dcoord' ])
3093
+ ordered_labels = scp .array (P ['ivl' ])
3094
+ color_list = scp .array (P ['color_list' ])
3095
+ colors = self .get_color_dict (colorscale )
3066
3096
3067
3097
trace_list = []
3068
3098
3069
3099
for i in range (len (icoord )):
3070
- # xs and ys are arrays of 4 points that make up the '∩' shapes of the dendrogram tree
3071
- xs = icoord [i ] if self .orientation in ['top' ,'bottom' ] else dcoord [i ]
3072
- ys = dcoord [i ] if self .orientation in ['top' ,'bottom' ] else icoord [i ]
3073
- color_key = color_list [i ]
3074
- trace = Scatter (x = np .multiply (self .sign [self .xaxis ],xs ), \
3075
- y = np .multiply (self .sign [self .yaxis ],ys ), \
3076
- mode = 'lines' , marker = Marker (color = colors [color_key ]) )
3100
+ # xs and ys are arrays of 4 points that make up the '∩' shapes
3101
+ # of the dendrogram tree
3102
+ if self .orientation in ['top' , 'bottom' ]:
3103
+ xs = icoord [i ]
3104
+ else :
3105
+ xs = dcoord [i ]
3106
+
3107
+ if self .orientation in ['top' , 'bottom' ]:
3108
+ ys = dcoord [i ]
3109
+ else :
3110
+ ys = icoord [i ]
3111
+ color_key = color_list [i ]
3112
+ trace = Scatter (x = np .multiply (self .sign [self .xaxis ], xs ),
3113
+ y = np .multiply (self .sign [self .yaxis ], ys ),
3114
+ mode = 'lines' ,
3115
+ marker = Marker (color = colors [color_key ]))
3077
3116
trace ['xaxis' ] = 'x' + self .xaxis [- 1 ]
3078
3117
trace ['yaxis' ] = 'y' + self .yaxis [- 1 ]
3079
- trace_list .append ( trace )
3080
-
3118
+ trace_list .append (trace )
3119
+
3081
3120
return trace_list , icoord , dcoord , ordered_labels , P ['leaves' ]
3082
3121
0 commit comments