小编Ami*_*Mek的帖子

使用 nx.degree_histogram 绘制图形的度数分布

我尝试使用以下代码来绘制 的度分布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])
Run Code Online (Sandbox Code Playgroud)

但后来我意识到这不是正确的做法,所以我将其更改为:

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()
Run Code Online (Sandbox Code Playgroud)

但这给了我一个没有数据的空图。

python directed-graph matplotlib histogram networkx

6
推荐指数
3
解决办法
2万
查看次数

标签 统计

directed-graph ×1

histogram ×1

matplotlib ×1

networkx ×1

python ×1