Skip to content

Commit 16c74c1

Browse files
committed
Cleanup of labels, axis and tests
1 parent 44cdece commit 16c74c1

File tree

2 files changed

+38
-14
lines changed

2 files changed

+38
-14
lines changed

plotly/tests/test_optional/test_opt_tracefactory.py

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,29 +115,29 @@ def test_default_dendrogram(self):
115115
[1, 2, 3, 1]]))
116116
expected_dendro_data = [{'marker': {'color': 'rgb(255,133,27)'},
117117
'mode': 'lines', 'xaxis': 'xs',
118-
'yaxis': 'ys',
118+
'yaxis': 'y',
119119
'y': np.array([0., 1., 1., 0.]),
120120
'x': np.array([25., 25., 35., 35.]),
121121
'type': u'scatter'},
122122
{'marker': {'color': 'rgb(255,133,27)'},
123123
'mode': 'lines',
124-
'xaxis': 'xs',
125-
'yaxis': 'ys',
124+
'xaxis': 'x',
125+
'yaxis': 'y',
126126
'y': np.array([0., 2.23606798,
127127
2.23606798, 1.]),
128128
'x': np.array([15., 15., 30., 30.]),
129129
'type': u'scatter'},
130130
{'marker': {'color': 'blue'},
131131
'mode': 'lines',
132-
'xaxis': 'xs',
133-
'yaxis': 'ys',
132+
'xaxis': 'x',
133+
'yaxis': 'y',
134134
'y': np.array([0., 3.60555128,
135135
3.60555128, 2.23606798]),
136136
'x': np.array([5., 5., 22.5, 22.5]),
137137
'type': u'scatter'}]
138138

139139
self.assertEqual(len(dendro['data']), len(expected_dendro_data))
140-
self.assertTrue(np.array_equal(dendro['labels'],
140+
self.assertTrue(np.array_equal(dendro['layout']['x']['ticktext'],
141141
np.array(['3', '2', '0', '1'])))
142142

143143
for i in range(1, len(dendro['data'])):
@@ -152,7 +152,32 @@ def test_dendrogram_random_matrix(self):
152152
# variable 2 is correlated with all the other variables
153153
X[2, :] = sum(X, 0)
154154

155-
dendro = tls.FigureFactory.create_dendrogram(X)
155+
names = ['Jack', 'Oxana', 'John', 'Chelsea', 'Mark']
156+
dendro = tls.FigureFactory.create_dendrogram(X, labels=names)
156157

157-
# Check that 2 is in a separate cluster
158-
self.assertEqual(dendro['labels'][0], '2')
158+
# Check that 2 is in a separate cluster and it's labelled correctly
159+
self.assertEqual(dendro['layout']['x']['ticktext'][0], 'John')
160+
161+
def test_dendrogram_orientation(self):
162+
X = np.random.rand(5, 5)
163+
164+
dendro_left = tls.FigureFactory.create_dendrogram(X, orientation='left')
165+
166+
self.assertEqual(len(dendro_left['layout']['y']['ticktext']), 5)
167+
tickvals_left = np.array(dendro_left['layout']['y']['tickvals'])
168+
self.assertTrue((tickvals_left <= 0).all())
169+
170+
dendro_right = tls.FigureFactory.create_dendrogram(X, orientation='right')
171+
tickvals_right = np.array(dendro_right['layout']['y']['tickvals'])
172+
self.assertTrue((tickvals_right >= 0).all())
173+
174+
dendro_bottom = tls.FigureFactory.create_dendrogram(X, orientation='bottom')
175+
self.assertEqual(len(dendro_bottom['layout']['x']['ticktext']), 5)
176+
tickvals_bottom = np.array(dendro_bottom['layout']['x']['tickvals'])
177+
self.assertTrue((tickvals_bottom >= 0).all())
178+
179+
dendro_top = tls.FigureFactory.create_dendrogram(X, orientation='top')
180+
tickvals_top = np.array(dendro_top['layout']['x']['tickvals'])
181+
self.assertTrue((tickvals_top <= 0).all())
182+
183+

plotly/tools.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2346,8 +2346,7 @@ def create_dendrogram(X, orientation="bottom", labels=None,
23462346
dendrogram = _Dendrogram(X, orientation, labels, colorscale)
23472347

23482348
return {'layout': dendrogram.layout,
2349-
'data': dendrogram.data,
2350-
'labels': dendrogram.labels}
2349+
'data': dendrogram.data}
23512350

23522351

23532352
class _Quiver(FigureFactory):
@@ -2953,7 +2952,7 @@ class _Dendrogram(FigureFactory):
29532952
"""
29542953

29552954
def __init__(self, X, orientation='bottom', labels=None, colorscale=None,
2956-
width="100%", height="100%", xaxis='xaxis', yaxis='yaxis'):
2955+
width="100%", height="100%", xaxis='x', yaxis='y'):
29572956
self.orientation = orientation
29582957
self.labels = labels
29592958
self.xaxis = xaxis
@@ -3125,8 +3124,8 @@ def get_dendrogram_traces(self, X, colorscale):
31253124
y=np.multiply(self.sign[self.yaxis], ys),
31263125
mode='lines',
31273126
marker=Marker(color=colors[color_key]))
3128-
trace['xaxis'] = 'x'+self.xaxis[-1]
3129-
trace['yaxis'] = 'y'+self.yaxis[-1]
3127+
trace['xaxis'] = self.xaxis
3128+
trace['yaxis'] = self.yaxis
31303129
trace_list.append(trace)
31313130

31323131
return trace_list, icoord, dcoord, ordered_labels, P['leaves']

0 commit comments

Comments
 (0)