I am using a GUI from QtDesigner to plot Dendrogram. My code is below, but I can not plot the Dendrogram, how can I fix it?
JavaScript
x
2
1
Error: 'AxesSubplot' object has no attribute 'dendrogram'
2
JavaScript
1
25
25
1
def dendro(self):
2
Z = linkage(X, method='ward', metric='euclidean', optimal_ordering=True)
3
c, coph_dists = cophenet(Z, pdist(X))
4
self.ui.linecoef.setText("%.3f" %c)
5
6
fig3 = Figure()
7
self.canvas3 = FigureCanvas(fig3)
8
self.ui.verticalLayout3.addWidget(self.canvas3)
9
fig3.subplots_adjust(top=0.93,bottom=0.125,left=0.1,right=0.97)
10
11
ax3f1 = fig3.add_subplot(121)
12
ax3f1.dendrogram(Z,leaf_rotation=90., leaf_font_size=8.,)
13
ax3f1.set_title('Hierarchical Clustering Dendrogram (full)')
14
ax3f1.set_xlabel('sample clusters')
15
ax3f1.set_ylabel('distance')
16
17
ax3f2 = fig3.add_subplot(122)
18
ax3f2.dendrogram(Z,truncate_mode='lastp', p=12, show_leaf_counts=True, show_contracted=True)
19
ax3f2.set_title('Hierarchical Clustering Dendrogram (truncated)')
20
ax3f2.set_xlabel('sample clusters size')
21
ax3f2.set_ylabel('distance')
22
23
self.canvas3.draw()
24
25
Advertisement
Answer
You have to import the dendrogram from scipy:
JavaScript
1
2
1
from scipy.cluster import hierarchy
2
And then pass it the axes through the ax argument:
JavaScript
1
21
21
1
ax3f1 = fig3.add_subplot(121)
2
hierarchy.dendrogram.dendrogram(
3
Z, ax=ax3f2, leaf_rotation=90.0, leaf_font_size=8.0
4
)
5
ax3f1.set_title("Hierarchical Clustering Dendrogram (full)")
6
ax3f1.set_xlabel("sample clusters")
7
ax3f1.set_ylabel("distance")
8
9
ax3f2 = fig3.add_subplot(122)
10
hierarchy.dendrogram(
11
Z,
12
ax=ax3f2,
13
truncate_mode="lastp",
14
p=12,
15
show_leaf_counts=True,
16
show_contracted=True,
17
)
18
ax3f2.set_title("Hierarchical Clustering Dendrogram (truncated)")
19
ax3f2.set_xlabel("sample clusters size")
20
ax3f2.set_ylabel("distance")
21