Gist · 1 / URL: https://code.ico2s.org/_admin/gists/1
Public Gist
Draw a graph of connections between grant proposal workpackages. Uses networkx and matplotlib.
Expires: Never
avatar
plw - created 11 years and 7 months ago
added file: graph.py
graph.py
#!/usr/bin/env python

import networkx
import matplotlib.pyplot as plt


edges = """
1.1 1.3 2.1 2.2 2.3 3.1 3.3 5.1 5.3
1.2 5.1 5.2
1.3 1.2 2.1 2.2 2.4 5.1 5.3

2.1 5.1 5.3
2.2 5.2 4.1
2.3 1.3 2.5 3.3 4.3 5.1 5.3
2.4 4.3 4.7
2.5 1.3 2.4 3.3 4.3 5.1 5.2 5.3

3.1
3.2 4.1 4.2 4.3 5.2
3.3 2.1 2.4 2.5 3.1 3.2 4.3

4.1 5.1 5.2 5.3 5.4 5.5
4.2 4.1 5.1 5.2 5.3 5.4
4.3 2.4 2.5 3.3 4.2 4.7
4.4 4.2 4.3 5.1 5.2 5.3 5.4
4.5 1.2 1.3 2.1 2.2 2.3 2.4 3.3 4.1 4.3
4.6 1.3 3.2 4.1 4.2 5.2
4.7 1.2 1.3 2.1 3.1 4.6
4.8 1.1 1.2 1.3 2.1 2.4 3.3 4.1 4.3 4.5 4.7 5.3 6.2 6.3

5.1 1.2 1.3 4.3 4.4 4.7 4.8
5.2 1.1 1.2 4.8
5.3 1.1 1.2 2.1 3.2 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 5.4
5.4 2.4 3.3 4.8
5.5 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8

6.1 1.1 4.8
6.2 1.1 4.8 6.1
6.3 6.1 6.2
"""

graph = networkx.Graph()

for line in edges.split("\n"):
	labels = line.split()
	if labels:
		node = labels[0]
		graph.add_node(node)
		for label in labels[1:]:
			graph.add_edge(node, label)

# color by first digit of the label
colors = [int(node[0]) for node in graph.nodes()]
# find graph layout using graphviz
positions = networkx.graphviz_layout(graph, "twopi")

networkx.draw_networkx_nodes(graph, positions, node_size=600, node_color=colors, cmap=plt.cm.Paired)
networkx.draw_networkx_edges(graph, positions, alpha=0.2, edge_color="#000000")
networkx.draw_networkx_labels(graph, positions)

# set plot limits
xmax = 1.05 * max(positions.values(), key=lambda x: x[0])[0]
ymax = 1.05 * max(positions.values(), key=lambda x: x[1])[1]
plt.xlim(0, xmax)
plt.ylim(0, ymax)

# set white background
plt.gcf().set_facecolor("w")

# do not show plot axes
plt.axis("off")

# show the graph
plt.show()