Last active
October 14, 2015 21:02
-
-
Save rbnvrw/c7571d92a6b0b8a9df2a to your computer and use it in GitHub Desktop.
Autolayout graph nodes for interrelation diagram
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from igraph import * | |
import numpy as np | |
# Data | |
edges = [(0,6),(0,7),(0,16),(0,19),(1,0),(1,3),(1,5),(1,16),(2,1),(2,3),(2,5),(2,11),(2,13),(2,14),(2,15),(2,16),(2,20),(2,22),(3,4),(3,12),(3,14),(3,16),(3,22),(4,22),(5,1),(5,2),(5,3),(5,6),(5,10),(5,11),(5,13),(5,14),(5,16),(5,20),(5,22),(5,23),(6,7),(6,19),(7,0),(7,4),(7,16),(7,18),(7,19),(8,0),(8,9),(8,16),(8,22),(9,0),(9,16),(10,0),(10,6),(11,0),(11,1),(11,3),(11,4),(11,7),(11,10),(11,13),(11,14),(11,16),(11,22),(12,0),(12,1),(12,6),(12,10),(12,11),(12,13),(12,14),(12,16),(12,20),(12,21),(12,22),(13,0),(13,6),(13,9),(13,14),(13,16),(13,18),(14,0),(14,1),(14,6),(14,9),(14,10),(14,20,),(14,22),(15,0),(15,3),(15,5),(15,8),(15,9),(15,12),(15,13),(15,16),(15,20),(15,21),(15,22),(15,23),(16,6),(16,7),(16,10),(16,14),(16,20),(16,22),(17,0),(17,2),(17,3),(17,8),(17,9),(17,10),(17,15),(17,22),(18,0),(18,11),(18,16),(18,19),(19,11),(20,6),(20,7),(20,19),(21,8),(21,9),(21,22),(22,4),(22,9),(22,19),(23,20)] | |
labels = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x"] | |
N = len(labels) | |
# Define colors used for outdegree visualization | |
colours = ['#ffffb2', '#fecc5c', '#fd8d3c', '#e31a1c'] | |
# Graph generation | |
g = Graph(vertex_attrs={"label": labels}, edges=edges, directed=True) | |
# Add in/outdegree to label | |
g.vs["label"] = [l+"\n\nIn: "+str(g.indegree(i))+" Out: "+str(g.outdegree(i)) for i,l in enumerate(g.vs["label"])] | |
# Calculate outdegree | |
degrees = g.outdegree() | |
# Order vertices in bins based on outdegree | |
bins = np.linspace(0, max(degrees), len(colours)) | |
digitized_degrees = np.digitize(degrees, bins) | |
# Set colors according to bins | |
g.vs["color"] = [colours[x-1] for x in digitized_degrees] | |
# Also color the edges | |
for ind, color in enumerate(g.vs["color"]): | |
edges = g.es.select(_source=ind) | |
edges["color"] = [color] | |
# Community detection | |
communities = g.community_edge_betweenness(directed=True) | |
clusters = communities.as_clustering() | |
# Alternative community detection | |
#clusters = g.community_infomap(vertex_weights=degrees, trials=1) | |
# Set edge weights based on communities | |
weights = {v: len(c) for c in clusters for v in c} | |
g.es["weight"] = [weights[e.tuple[0]] + weights[e.tuple[1]] for e in g.es] | |
# Set visual style for the plot | |
visual_style = {} | |
# Choose the layout | |
visual_style["layout"] = g.layout_fruchterman_reingold(weights=g.es["weight"], maxiter=1000, area=N**3, repulserad=N**3) | |
# Other options | |
visual_style["vertex_shape"] = "rectangle" | |
visual_style["vertex_size"] = 85 | |
visual_style["vertex_label_dist"] = 0 | |
visual_style["vertex_label_size"] = 8 | |
visual_style["vertex_frame_color"] = g.vs["color"] | |
visual_style["vertex_label_family"] = "Calibri" | |
visual_style["wrap_labels"] = True | |
visual_style["bbox"] = (764,875) | |
visual_style["margin"] = 50 | |
# Plot the graph | |
plot(g, target="diagram.svg", **visual_style) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment