Skip to content

Instantly share code, notes, and snippets.

@alexeyev
Created May 29, 2015 22:08
Show Gist options
  • Save alexeyev/8073e9a8d2854af79800 to your computer and use it in GitHub Desktop.
Save alexeyev/8073e9a8d2854af79800 to your computer and use it in GitHub Desktop.
Beautiful two-communities-graph layout
import igraph
clusters_const = 2
graph = igraph.read("2_big_graph_clusters_ncol.txt", format="ncol", directed=False, names=True)
clusters = graph.community_edge_betweenness(clusters=2, directed=False)
splitter = clusters.as_clustering(clusters_const).membership
vs = igraph.VertexSeq(graph)
def get_community_map(community_id, color_to_be_set):
map = {}
for v, index in zip(vs, range(len(vs))): #todo: get vertices ids from igraph.Vertex, not zipping
if splitter[index] == community_id:
map[index] = v['name']
v['color'] = color_to_be_set
v['label'] = v['name']
return map
map_1 = get_community_map(0, (0.9, 1.0, 0.9))
map_2 = get_community_map(1, (0.9, 0.9, 1.0))
# printing communities labels
print "community of size", len(map_1), ",".join(map_1.values())
print "community of size", len(map_2), ",".join(map_2.values())
# printing intercluster connections
for c1 in map_1.keys():
for c2 in map_2.keys():
if graph.get_eid(c1, c2, directed=False, error=False) != -1:
print map_1[c1], "--", map_2[c2],
# See for tuning http://igraph.org/python/doc/igraph.Graph-class.html#__plot__
igraph.plot(graph, vertex_frame_width=0.5, vertex_frame_color="gray", edge_width=0.5, edge_color="gray")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment