-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreference tree plotter.py
More file actions
39 lines (33 loc) · 1.29 KB
/
Copy pathreference tree plotter.py
File metadata and controls
39 lines (33 loc) · 1.29 KB
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
import igraph as ig
import matplotlib.pyplot as plt
# Construct a graph with 3 vertices
n_vertices = len(vertex_set)
edges = [(0, 1), (0, 2), (0, 3), (0, 4), (1, 2), (1, 3), (1, 4), (3, 4)]
g = ig.Graph(n_vertices, edges)
# Set attributes for the graph, nodes, and edges
g["title"] = "Small Social Network"
g.vs["name"] = vertex_set_as_str
g.vs["gender"] = ["M", "F", "F", "M", "F"]
g.es["married"] = [False, False, False, False, False, False, False, True]
# Set individual attributes
# g.vs[1]["name"] = "Kathy Morillas"
# g.es[0]["married"] = True
# Plot in matplotlib
# Note that attributes can be set globally (e.g. vertex_size), or set individually using arrays (e.g. vertex_color)
fig, ax = plt.subplots(figsize=(5,5))
ig.plot(
g,
target=ax,
layout = g.layout_reingold_tilford(mode="in", root=[0]), # print nodes in a circular layout
vertex_size=0.1,
vertex_color=["steelblue" if gender == "M" else "salmon" for gender in g.vs["gender"]],
vertex_frame_width=4.0,
vertex_frame_color="white",
vertex_label=g.vs["name"],
vertex_label_size=7.0,
edge_width=[2 if married else 1 for married in g.es["married"]],
edge_color=["#7142cf" if married else "#AAA" for married in g.es["married"]]
)
plt.show()
# Save the graph as an image file
fig.savefig('social_network.png')