如何在 GUI 中绘制 DENDROGRAM
How to plot DENDROGRAM in GUI
我正在使用 QtDesigner 的 GUI 绘制树状图。我的代码如下,但我无法绘制树状图,我该如何解决?
Error: 'AxesSubplot' object has no attribute 'dendrogram'
def dendro(self):
Z = linkage(X, method='ward', metric='euclidean', optimal_ordering=True)
c, coph_dists = cophenet(Z, pdist(X))
self.ui.linecoef.setText("%.3f" %c)
fig3 = Figure()
self.canvas3 = FigureCanvas(fig3)
self.ui.verticalLayout3.addWidget(self.canvas3)
fig3.subplots_adjust(top=0.93,bottom=0.125,left=0.1,right=0.97)
ax3f1 = fig3.add_subplot(121)
ax3f1.dendrogram(Z,leaf_rotation=90., leaf_font_size=8.,)
ax3f1.set_title('Hierarchical Clustering Dendrogram (full)')
ax3f1.set_xlabel('sample clusters')
ax3f1.set_ylabel('distance')
ax3f2 = fig3.add_subplot(122)
ax3f2.dendrogram(Z,truncate_mode='lastp', p=12, show_leaf_counts=True, show_contracted=True)
ax3f2.set_title('Hierarchical Clustering Dendrogram (truncated)')
ax3f2.set_xlabel('sample clusters size')
ax3f2.set_ylabel('distance')
self.canvas3.draw()
树状图图像:
您必须从 scipy:
导入树状图
from scipy.cluster import hierarchy
然后通过 ax 参数将轴传递给它:
ax3f1 = fig3.add_subplot(121)
hierarchy.dendrogram.dendrogram(
Z, ax=ax3f2, leaf_rotation=90.0, leaf_font_size=8.0
)
ax3f1.set_title("Hierarchical Clustering Dendrogram (full)")
ax3f1.set_xlabel("sample clusters")
ax3f1.set_ylabel("distance")
ax3f2 = fig3.add_subplot(122)
hierarchy.dendrogram(
Z,
ax=ax3f2,
truncate_mode="lastp",
p=12,
show_leaf_counts=True,
show_contracted=True,
)
ax3f2.set_title("Hierarchical Clustering Dendrogram (truncated)")
ax3f2.set_xlabel("sample clusters size")
ax3f2.set_ylabel("distance")
我正在使用 QtDesigner 的 GUI 绘制树状图。我的代码如下,但我无法绘制树状图,我该如何解决?
Error: 'AxesSubplot' object has no attribute 'dendrogram'
def dendro(self):
Z = linkage(X, method='ward', metric='euclidean', optimal_ordering=True)
c, coph_dists = cophenet(Z, pdist(X))
self.ui.linecoef.setText("%.3f" %c)
fig3 = Figure()
self.canvas3 = FigureCanvas(fig3)
self.ui.verticalLayout3.addWidget(self.canvas3)
fig3.subplots_adjust(top=0.93,bottom=0.125,left=0.1,right=0.97)
ax3f1 = fig3.add_subplot(121)
ax3f1.dendrogram(Z,leaf_rotation=90., leaf_font_size=8.,)
ax3f1.set_title('Hierarchical Clustering Dendrogram (full)')
ax3f1.set_xlabel('sample clusters')
ax3f1.set_ylabel('distance')
ax3f2 = fig3.add_subplot(122)
ax3f2.dendrogram(Z,truncate_mode='lastp', p=12, show_leaf_counts=True, show_contracted=True)
ax3f2.set_title('Hierarchical Clustering Dendrogram (truncated)')
ax3f2.set_xlabel('sample clusters size')
ax3f2.set_ylabel('distance')
self.canvas3.draw()
树状图图像:
您必须从 scipy:
导入树状图from scipy.cluster import hierarchy
然后通过 ax 参数将轴传递给它:
ax3f1 = fig3.add_subplot(121)
hierarchy.dendrogram.dendrogram(
Z, ax=ax3f2, leaf_rotation=90.0, leaf_font_size=8.0
)
ax3f1.set_title("Hierarchical Clustering Dendrogram (full)")
ax3f1.set_xlabel("sample clusters")
ax3f1.set_ylabel("distance")
ax3f2 = fig3.add_subplot(122)
hierarchy.dendrogram(
Z,
ax=ax3f2,
truncate_mode="lastp",
p=12,
show_leaf_counts=True,
show_contracted=True,
)
ax3f2.set_title("Hierarchical Clustering Dendrogram (truncated)")
ax3f2.set_xlabel("sample clusters size")
ax3f2.set_ylabel("distance")