Python Networks 绘制网络的度分布

Python Networks 绘制网络的度分布

# 加载然后进行可视化
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np


G1 = nx.read_adjlist(G1path)
G2 = nx.read_adjlist(G2path)

# 对这个随机网络进行分析
# 计算网络的度分布  # 绘制网络的度分布;
degree_sequence_g1 = sorted((d for n, d in G1.degree()), reverse=True)
degree_sequence_g2 = sorted((d for n, d in G2.degree()), reverse=True)

fig, ax = plt.subplots()
ax.bar(*np.unique(degree_sequence_g1, return_counts=True),color='b',alpha=0.3,label='Star Network')
ax.bar(*np.unique(degree_sequence_g2, return_counts=True),color='r',alpha=0.3,label='Random Network')
ax.set_ylim(ymin=0, ymax=35)
ax.set_title("Degree histogram")
ax.set_xlabel("Degree")
ax.set_ylabel("# of Nodes")
ax.legend(loc = "best")
plt.show()
print('plot Degree histogram graph finished!')

 

 

posted @ 2022-08-25 10:45  bH1pJ  阅读(133)  评论(0编辑  收藏  举报