import numpy as np
import networkx as nx
import numpy.linalg as la
import scipy.cluster.vq as vq
import matplotlib.pyplot as plt

G = nx.powerlaw_cluster_graph(100, 1, 0.0)
coord = nx.spring_layout(G, iterations=1000)


fig = plt.figure()
axs = fig.add_subplot(111, aspect='equal')
axs.axis('off')
nx.draw_networkx_edges(G, coord)
nx.draw_networkx_nodes(G, coord, node_size=45, node_color='k')
plt.show()


A = nx.adjacency_matrix(G)
D = np.diag(np.ravel(np.sum(A, axis=1)))
L = D - A

l, U = la.eigh(L)
f = U[:,1]

# labels = np.ravel(np.sign(f))
k = 3
means, labels = vq.kmeans2(U[:,1:k], k)

fig = plt.figure()
nx.draw_networkx_edges(G, coord)
nx.draw_networkx_nodes(G, coord, node_size=45, node_color=labels)
plt.show()
#######################################
1
1.39034041
-0.018145916
2
-1.13626889
-1.093869264
3
1.19427580
0.205195920
4
1.66639609
0.348523736
5
0.13588088
-1.963015024
6
-0.09012193
-1.849551377
7
1.40749495
0.551643582
8
0.64164912
3.292957411
9
-0.69657885
-1.545402433
10
1.22879663
0.714832605
11
1.00406453
0.828158819
12
1.12273869
1.086430066
13
1.20052548
1.398172065
14
0.38890918
-2.109159025
15
0.94309052
-2.026132972
16
0.82657147
1.373363785
17
0.63141154
1.463102208
18
0.63639811
1.875192367
19
0.35518424
1.875320476
20
-1.45733601
3.059970190
21
2.72989626
0.694060502
22
0.13297292
2.001611963
23
2.24825530
-1.260481774
24
1.32079199
2.817808855
25
2.72588334
-0.009424627
26
1.49641499
-1.829149543
27
2.31973689
-0.916159996
28
-0.13614033
1.961220722
29
-0.39142788
1.912183698
30
2.41284377
-0.607196638
31
-0.64542059
1.912162507
32
1.98240724
2.383195466
33
-2.70804615
2.549216875
34
-0.90350288
1.869867098
35
1.84436630
-1.309853811
36
2.09428075
1.986220501
37
-1.38493526
-0.358460276
38
0.68333410
-2.232574761
39
-1.06882060
3.284475448
40
-1.17727088
-0.895146271
41
-4.06785644
0.530759369
42
-1.14995936
1.764730394
43
2.45823742
1.363059740
44
-1.48523566
1.809076806
45
-1.75483093
1.666010453
46
-3.48063665
1.366087914
47
1.04058452
3.151567968
48
-1.94357265
1.412687101
49
2.58256824
0.344285079
50
-1.49114826
-0.560044273
51
-0.62954947
-1.863707884
52
-2.17576508
1.202583102
53
-0.37677193
-1.925531656
54
-2.38572922
0.956141846
55
-2.70697771
0.737006377
56
-3.79825053
0.978480535
57
2.64812361
1.045194872
58
-2.78072655
0.382956228
59
-2.68559478
0.003012502
60
-1.42250126
0.028383034
61
-2.85374058
-0.336385648
62
-2.77717626
-0.698779110
63
-0.62538152
3.133807152
64
-2.66310297
-1.048669942
65
-2.54017284
-1.393558768
66
-3.51062570
1.959899874
67
-2.29814622
-1.684697527
68
1.76946762
2.744236379
69
-2.25720825
-2.100779993
70
-2.07101296
-2.464252131
71
-1.66542654
-2.621174190
72
-1.31350401
-0.184563846
73
-3.07113467
2.240928693
74
2.47653345
-0.293286134
75
-1.33274013
-2.836290936
76
-0.97951814
-3.023855272
77
-0.65487047
-3.333093559
78
-0.21538455
-3.340371636
79
0.20525788
-3.444281806
80
0.64885579
-3.495609517
81
1.13631163
-1.730385151
82
1.06397033
-3.259569834
83
0.20675117
3.295597900
84
1.50510375
-3.204196018
85
1.87981931
-2.944375621
86
2.32773903
-2.819125701
87
2.29112426
1.681000397
88
2.77332343
-2.626404262
89
-2.35952594
2.868213275
90
3.09582743
-2.263544069
91
3.35719005
-1.851682680
92
-0.20227236
3.491747191
93
1.71146954
-1.607514344
94
3.51110403
-1.382224315
95
-0.91599736
-1.459849091
96
-1.85297223
2.888408082
97
-1.12244996
-1.340536214
98
-1.39307069
-0.754136230
99
3.78592031
-0.973840685
100
3.87830679
-0.485819544