Skip to content

Commit fac00f1

Browse files
444thLiaojonmmease
authored andcommitted
fix wrong dendrogram leaves determination bug (#1186)
* fix wrong leaves determination bug * add test_dendrogram_ticklabels and comments about why/how to fix the dendrogram bugs.
1 parent f727c46 commit fac00f1

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

Diff for: plotly/figure_factory/_dendrogram.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,20 @@ def __init__(self, X, orientation='bottom', labels=None, colorscale=None,
141141
if yvals_flat[i] == 0.0 and xvals_flat[i] not in self.zero_vals:
142142
self.zero_vals.append(xvals_flat[i])
143143

144-
self.zero_vals.sort()
144+
if len(self.zero_vals) > len(yvals) + 1:
145+
# If the length of zero_vals is larger than the length of yvals,
146+
# it means that there are wrong vals because of the identicial samples.
147+
# Three and more identicial samples will make the yvals of spliting center into 0 and it will \
148+
# accidentally take it as leaves.
149+
l_border = int(min(self.zero_vals))
150+
r_border = int(max(self.zero_vals))
151+
correct_leaves_pos = range(l_border,
152+
r_border + 1,
153+
int((r_border - l_border) / len(yvals)))
154+
# Regenerating the leaves pos from the self.zero_vals with equally intervals.
155+
self.zero_vals = [v for v in correct_leaves_pos]
145156

157+
self.zero_vals.sort()
146158
self.layout = self.set_figure_layout(width, height)
147159
self.data = dd_traces
148160

Diff for: plotly/tests/test_optional/test_figure_factory/test_figure_factory.py

+9
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,15 @@ def test_dendrogram_colorscale(self):
777777
self.assert_fig_equal(dendro['data'][1], expected_dendro['data'][1])
778778
self.assert_fig_equal(dendro['data'][2], expected_dendro['data'][2])
779779

780+
def test_dendrogram_ticklabels(self):
781+
X = np.array([[1, 2, 3, 4], [1, 2, 3, 4], [1, 3, 5, 6], [1, 4, 2, 3]])
782+
dendro = ff.create_dendrogram(X=X)
783+
784+
expected_ticktext = ['2', '3', '0', '1']
785+
expected_tickvals = [5, 15, 25, 35]
786+
787+
self.assertEqual(len(dendro.layout.xaxis.ticktext), 4)
788+
self.assertEqual(len(dendro.layout.xaxis.tickvals), 4)
780789

781790
class TestTrisurf(NumpyTestUtilsMixin, TestCase):
782791

0 commit comments

Comments
 (0)