我尝试使用以下代码绘制 networkx.DiGraph
G
的度数分布:
def plot_degree_In(G):
in_degrees = G.in_degree()
in_degrees=dict(in_degrees)
in_values = sorted(set(in_degrees.values()))
in_hist = [list(in_degrees.values()).count(x) for x in in_values]
plt.figure()
plt.grid(False)
plt.loglog(in_values, in_hist, 'r.')
#plt.loglog(out_values, out_hist, 'b.')
#plt.legend(['In-degree', 'Out-degree'])
plt.xlabel('k')
plt.ylabel('p(k)')
plt.title('Degree Distribution')
plt.xlim([0, 2*100**1])
但是后来我意识到这不是正确的方法,所以我将其更改为:
def plot_degree_dist(G):
degree_hist = nx.degree_histogram(G)
degree_hist = np.array(degree_hist, dtype=float)
degree_prob = degree_hist/G.number_of_nodes()
plt.loglog(np.arange(degree_prob.shape[0]),degree_prob,'b.')
plt.xlabel('k')
plt.ylabel('p(k)')
plt.title('Degree Distribution')
plt.show()
但这给了我一个没有数据的空图。
原文由 Amit Mek 发布,翻译遵循 CC BY-SA 4.0 许可协议
一种使用测试代码打印(进-加出-)度直方图的方法:
可以通过向
plt.hist
添加第二个参数来调整直方图的 bin 数。