-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_dendrogram.py
More file actions
executable file
·62 lines (51 loc) · 1.54 KB
/
plot_dendrogram.py
File metadata and controls
executable file
·62 lines (51 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python
# https://stackoverflow.com/questions/2455761/reordering-matrix-elements-to-reflect-column-and-row-clustering-in-naiive-python
import numpy as np
import pylab
import scipy.cluster.hierarchy as sch
import pandas as pd
import sys
def load_df(name):
df = pd.read_csv(name, header=None)
df = df.drop(columns=445)
return df
def get_distance_matrix(x):
D = np.zeros(x.shape)
for i in range(len(x)):
for j in range(len(x)):
D[i,j] = sum((x[i,:] - x[j,:]) ** 2) ** 0.5
return D
def clique_p(n, v):
r = np.zeros((n, n))
r[:,:] = v
for i in range(n):
r[i,i] = 1.0
return r
def plot_dendro(x):
D = get_distance_matrix(x)
# Compute and plot dendrogram.
fig = pylab.figure()
axdendro = fig.add_axes([0.09,0.1,0.2,0.8])
Y = sch.linkage(x, method='centroid')
Z = sch.dendrogram(Y, orientation='right')
axdendro.set_xticks([])
axdendro.set_yticks([])
# Plot distance matrix.
axmatrix = fig.add_axes([0.3,0.1,0.6,0.8])
index = Z['leaves']
D = D[index,:]
D = D[:,index]
im = axmatrix.matshow(D, aspect='auto', origin='lower')
axmatrix.set_xticks([])
axmatrix.set_yticks([])
# Plot colorbar.
axcolor = fig.add_axes([0.91,0.1,0.02,0.8])
pylab.colorbar(im, cax=axcolor)
# Display and save figure.
fig.show()
fig.savefig('dendrogram.png')
if __name__ == '__main__':
if len(sys.argv) < 2:
print("Usage:\n\t%s csv-file" % sys.argv[0])
sys.exit(1)
plot_dendro(np.array(load_df(sys.argv[1])))