【问题标题】:Control the height of dendrograms python控制树状图python的高度
【发布时间】:2015-03-23 12:40:36
【问题描述】:

如何修改scipy 在层次聚类期间计算的树状图的高度?

MWE

 import scipy
 import pylab
 import scipy.cluster.hierarchy as sch

 # Generate random features and distance matrix.
 x = scipy.rand(40)
 D = scipy.zeros([40,40])
 for i in range(40):
      for j in range(40):
         D[i,j] = abs(x[i] - x[j])

 # Compute and plot first dendrogram.
fig = pylab.figure(figsize=(8,8))
ax1 = fig.add_axes([0.09,0.1,0.2,0.6])
Y = sch.linkage(D, method='centroid')
Z1 = sch.dendrogram(Y, orientation='right')
ax1.set_xticks([])
ax1.set_yticks([])

# Compute and plot second dendrogram.
ax2 = fig.add_axes([0.3,0.71,0.6,0.2])
Y = sch.linkage(D, method='single')
Z2 = sch.dendrogram(Y)
ax2.set_xticks([])
ax2.set_yticks([])

# Plot distance matrix.
axmatrix = fig.add_axes([0.3,0.1,0.6,0.6])
idx1 = Z1['leaves']
idx2 = Z2['leaves']
D = D[idx1,:]
D = D[:,idx2]
im = axmatrix.matshow(D, aspect='auto', origin='lower', cmap=pylab.cm.YlGnBu)
axmatrix.set_xticks([])
axmatrix.set_yticks([])

# Plot colorbar.
axcolor = fig.add_axes([0.91,0.1,0.02,0.6])
pylab.colorbar(im, cax=axcolor)
fig.show()
fig.savefig('dendrogram.png')

【问题讨论】:

    标签: python matplotlib scipy


    【解决方案1】:

    您应该能够将轴应用于树状图,然后像对待任何其他绘图一样对待它们。目前,您似乎已经制作了轴,但没有将其添加到树状图中。如果你使用

    会发生什么
    Z1 = sch.dendrogram(Y, orientation='right', ax=ax1)
    

    等等。在你的电话中?

    【讨论】:

      猜你喜欢
      • 2022-01-18
      • 2014-04-14
      • 2010-11-25
      • 2016-07-31
      • 2015-02-12
      • 1970-01-01
      • 1970-01-01
      • 2021-06-07
      • 1970-01-01
      相关资源
      最近更新 更多