-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_igraph.py
60 lines (51 loc) · 1.53 KB
/
test_igraph.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Dec 25 21:53:09 2023
@author: Bing.Han
"""
import igraph as ig
import matplotlib.pyplot as plt
import os
def plot_net():
# # Set up workspace
# path="igraph"
# os.chdir(path)
# Read data
# g= ig.load("netdata/adjnoun/adjnoun.gml")
# g= ig.load("netdata/celegansneural/celegansneural.gml")
# g= ig.load("netdata/football/football.gml")
# g= ig.load("netdata/lesmis/lesmis.gml")
g= ig.load("netdata/polbooks/polbooks.gml")
print(g)
# Community partitioning
communities =g.community_edge_betweenness()
communities =communities.as_clustering()
# View community partitioning results
for i, community in enumerate(communities):
print(f"Community {i}:")
for v in community:
print(f"\t{g.vs[v]['label']}")
# Community color Settings
num_communities =len(communities)
palette1= ig.RainbowPalette(n=num_communities)
for i, community in enumerate(communities):
g.vs[community]["color"]= i
community_edges =g.es.select(_within=community)
community_edges["color"]=i
g.vs["label"]=["\n\n" + label for label in g.vs["label"]]
# Visualization
layout = g.layout("kk")
fig1, ax1 = plt.subplots()
ig.plot(
communities,
layout = layout,
target = ax1,
mark_groups = True,
palette = palette1,
vertex_size = 20,
edge_width = 1.0,
vertex_label_size = 10
)
fig1.set_size_inches(4,4)
plt.show()