networkx是针对复杂网络研发的库,可以通过在cmd窗口中pip 或者直接在pycharm中通过:File—>Setting-->Project-->Project interpreter 这样的路径可以看到你目前所安装的库,并且点击右上方的“+”,能够在搜索框中搜索你想安装的库,点击下方“install package”就可以安装相关库。本人是在pycharm中安装完成,非常简单快速。
在运用networkx时会用到numpy库和matplotlib库,可以进行pip安装,在安装matplotlib时最后会自动安装numpy.
python -m pip install matplotlib, 如果超时报错‘’raise ReadTimeoutError '':pip install -U --timeout 1000 matplotlib,
如果报错”these package do not match the hashes.....“:pip install --upgrade matplotlib
import networkx as nx import matplotlib.pyplot as plt G = nx.Graph() G.add_nodes_from(['a', 'b', 'c', 'd', 'e', 'f', 'g']) G.add_edges_from([('a', 'c'), ('b', 'c'), ('c', 'd'), ('d', 'e'), ('d', 'f'), ('d', 'g')]) nx.draw(G, with_labels=True) # 绘制网图 plt.show() degree = nx.degree_histogram(G) # 绘制度分布序列图 x = range(len(degree)) y = [z/float(sum(degree)) for z in degree] plt.loglog(x, y, color='yellow', linewidth=3) plt.show() print('节点', G.nodes()) print('边数', G.number_of_edges()) print('每个节点的度', G.degree()) print('度分布序列', nx.degree_histogram(G)) print('每个节点的聚集系数', nx.clustering(G)) print('平均聚集系数', nx.average_clustering(G)) print('图的直径', nx.diameter(G)) print('富人俱乐部系数'nx.rich_club_coefficient(G))