2016-06-23 14 views
1

私の仕事ではthis postのデンドログラムを使っていますが、どの行/列がどのデータから来ているのかを把握したいと思っています。名前付きスキピドンドログラム

データの名前のレコードをコードとして編集しました。namesとして以下のように編集しました。距離行列の視覚化の右下に名前を表示したいと思います。私はdendrogramへの呼び出しでlabels = namesを追加しようとしましたが、これは役に立ちませんでした。

これにラベルを追加する方法を知っている人はいますか?

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]) 

### new code 
names = [ ] 
for i in range(40): 
    names.append('str%i'%(i)) 
    print names[-1] 
### end new code 

# 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

私が書いたPythonパッケージheatmapcluster(利用可能on PyPI)は、ラベル(実際には、必要があります)を受け入れます。ここで

heatmapclusterを使用してスクリプトの簡易版です。

import numpy as np 
import matplotlib.pyplot as plt 
from heatmapcluster import heatmapcluster 


# Generate random features and distance matrix. 
x = np.random.rand(40) 
D = np.abs(np.subtract.outer(x, x)) 

names = ['str%i' % i for i in range(len(x))] 

h = heatmapcluster(D, names, names, 
        num_row_clusters=3, num_col_clusters=3, 
        label_fontsize=8, 
        xlabel_rotation=-75, 
        cmap=plt.cm.coolwarm, 
        show_colorbar=True, 
        top_dendrogram=True) 

plt.show() 

そして、ここではそれが生成するプロットである: plot

(なしDのような対称配列のために、実際にそこにある、ということに注意してください対称性により、同じ樹形図を生成する)

関連する問題