首先要安装 networkx
import matplotlib.pyplot as plt
import networkx as nx
import dgl
import numpy as np
def build_karate_club_graph():src = np.array([1, 2, 2, 3, 3])dst = np.array([0, 0, 1, 0, 1])u = np.concatenate([src, dst])v = np.concatenate([dst, src])return dgl.DGLGraph((u, v))
# 1.构建好dgl图
G = build_karate_club_graph()
# 2.将dgl图转化为networkx图
nx_G1 = g1.to_networkx().to_undirected() # 无向图
nx_G2 = g1.to_networkx() # 有向图
# 3.画图
plt.subplot(121)
#nx.draw(nx_G, with_labels=True, font_weight='bold') # 节点无顺序
nx.draw_shell(nx_G1, with_labels=True, font_weight='bold') # 节点按序排列
plt.subplot(122)
nx.draw_shell(nx_G2, with_labels=True, font_weight='bold') # 节点按序排列
plt.show()
例如: