def draw_adjacency_matrix(G, node_order=None, partitions=[], colors=[]):
"""
- G is a networkx graph
- node_order (optional) is a list of nodes, where each node in G
appears exactly once
- partitions is a list of node lists, where each node in G appears
in exactly one node list
- colors is a list of strings indicating what color each
partition should be
If partitions is specified, the same number of colors needs to be
specified.
"""
adjacency_matrix = nx.to_numpy_matrix(G, dtype=np.bool, nodelist=node_order)
# Plot adjacency matrix in toned-down black and white
fig = plt.figure(figsize=(8, 8)) # in inches
plt.imshow(adjacency_matrix,
cmap="Paired",
interpolation="none")
# The rest is just if you have sorted nodes by a partition and want to
# highlight the module boundaries
assert len(partitions) == len(colors)
ax = plt.gca()
for partition, color in zip(partitions, colors):
current_idx = 0
for module in partition:
ax.add_patch(patches.Rectangle((current_idx, current_idx),
len(module), # Width
len(module), # Height
facecolor="none",
edgecolor=color,
linewidth="1"))
current_idx += len(module)
plt.show()
评论列表
文章目录