图形绘制

networkx主要不是一个图形绘制包,而是一个带有matplotlib的基本绘图,以及一个使用开源graphviz软件包的接口。这些是 networkx.drawing 模块,如果可能,将导入。

首先导入Matplotlib的绘图接口(Pylab也可以工作)

1
import matplotlib.pyplot as plt

测试是否导入 networkx.drawing 抽签成功 G 使用其中之一

1
2
3
4
5
6
7
G = nx.petersen_graph()
plt.subplot(121)
<matplotlib.axes._subplots.AxesSubplot object at ...>
nx.draw(G, with_labels=True, font_weight='bold')
plt.subplot(122)
<matplotlib.axes._subplots.AxesSubplot object at ...>
nx.draw_shell(G, nlist=[range(5, 10), range(5)], with_labels=True, font_weight='bold')