Skip to content

Instantly share code, notes, and snippets.

@Christopher-Thornton
Last active August 4, 2020 19:10
Show Gist options
  • Save Christopher-Thornton/03f17a66519c5e5e02cf9d7aef875f82 to your computer and use it in GitHub Desktop.
Save Christopher-Thornton/03f17a66519c5e5e02cf9d7aef875f82 to your computer and use it in GitHub Desktop.
def filter_graph(pairs, node):
k_graph = nx.from_pandas_edgelist(pairs, 'subject', 'object',
create_using=nx.MultiDiGraph())
edges = nx.dfs_successors(k_graph, node)
nodes = []
for k, v in edges.items():
nodes.extend([k])
nodes.extend(v)
subgraph = k_graph.subgraph(nodes)
layout = (nx.random_layout(k_graph))
nx.draw_networkx(
subgraph,
node_size=1000,
arrowsize=20,
linewidths=1.5,
pos=layout,
edge_color='red',
edgecolors='black',
node_color='white'
)
labels = dict(zip((list(zip(pairs.subject, pairs.object))),
pairs['relation'].tolist()))
edges= tuple(subgraph.out_edges(data=False))
sublabels ={k: labels[k] for k in edges}
nx.draw_networkx_edge_labels(subgraph, pos=layout, edge_labels=sublabels,
font_color='red')
plt.axis('off')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment