首页 > 其他 > 详细

graph degree histogram

时间:2019-10-10 13:58:33      阅读:135      评论:0      收藏:0      [点我收藏+]

图的度数分布

import collections
import matplotlib.pyplot as plt
import networkx as nx

G = nx.gnp_random_graph(100, 0.02)

degree_sequence = sorted([d for n, d in G.degree()], reverse=True)  # degree sequence
# print "Degree sequence", degree_sequence
degreeCount = collections.Counter(degree_sequence)
deg, cnt = zip(*degreeCount.items())

# #as an alternation, you can pick out the top N items for the plot:
#d = sorted(degreeCount.items(), key=lambda item:item[1], reverse=True)[:30]   # pick out the up 30 items from counter
#deg = [i[0] for i in d]
#cnt = [i[1] for i in d]

fig, ax = plt.subplots()
plt.bar(deg, cnt, width=0.80, color='b')

plt.title("Degree Histogram")
plt.ylabel("Count")
plt.xlabel("Degree")
ax.set_xticks([d + 0.4 for d in deg])
ax.set_xticklabels(deg)

# draw graph in inset
plt.axes([0.4, 0.4, 0.5, 0.5])
Gcc = sorted(nx.connected_component_subgraphs(G), key=len, reverse=True)[0]
pos = nx.spring_layout(G)
plt.axis('off')
nx.draw_networkx_nodes(G, pos, node_size=20)
nx.draw_networkx_edges(G, pos, alpha=0.4)

Source for reference:
degree-histogram, networkx
技术分享图片

graph degree histogram

原文:https://www.cnblogs.com/sonictl/p/11647416.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!