控制树状图python的高度

时间:2015-03-23 12:40:37

标签: python matplotlib scipy

如何修改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')

1 个答案:

答案 0 :(得分:0)

您应该能够将轴应用于树形图,然后像任何其他图一样对待它们。此刻,它看起来好像你已经制作了轴,但没有将它添加到树形图中。如果您使用

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

等。在你的电话中?