Skip to content

Instantly share code, notes, and snippets.

@Christopher-Thornton
Last active August 1, 2021 14:15
Show Gist options
  • Save Christopher-Thornton/378d33fc0ef888a1015f9a7074244a20 to your computer and use it in GitHub Desktop.
Save Christopher-Thornton/378d33fc0ef888a1015f9a7074244a20 to your computer and use it in GitHub Desktop.
import networkx as nx
import matplotlib.pyplot as plt
def draw_kg(pairs):
k_graph = nx.from_pandas_edgelist(pairs, 'subject', 'object',
create_using=nx.MultiDiGraph())
node_deg = nx.degree(k_graph)
layout = nx.spring_layout(k_graph, k=0.15, iterations=20)
plt.figure(num=None, figsize=(120, 90), dpi=80)
nx.draw_networkx(
k_graph,
node_size=[int(deg[1]) * 500 for deg in node_deg],
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()))
nx.draw_networkx_edge_labels(k_graph, pos=layout, edge_labels=labels,
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