简体   繁体   English

Python:获取所有节点的度数,然后在networkx中绘制箱线图

[英]Python: get the degree of all nodes, then draw a boxplot in networkx

I have a homework that need to call Networkx's function to get the degrees of all the nodes, and then draw a boxplot for these degrees.我有一个作业需要调用Networkx的函数获取所有节点的度数,然后为这些度数绘制箱线图。

But the boxplot is not show and have a error below:但是箱线图没有显示并且在下面有错误:

"degree_values = list(my_degrees.values()); “degree_values = list(my_degrees.values());

AttributeError: 'DegreeView' object has no attribute 'values'" AttributeError: 'DegreeView' 对象没有属性 'values'”

How to solve this problem?如何解决这个问题呢? Thanks.谢谢。

import networkx as nx
import matplotlib.pyplot as plt

G = nx.Graph()
G.add_edges_from([['9606.EN01','9606.EN02'],['9606.EN01','9606.EN03']])

fig = plt.figure();
nx.draw(G, with_labels=True, font_weight='bold')
plt.draw()

my_degrees = G.degree();
degree_values = list(my_degrees.values());
fig = plt.figure();
plt.boxplot(degree_values)

DegreeView isn't a dictionary (in NetworkX 2.1), but it is an iterator over (node, degree) pairs. DegreeView不是字典(在 NetworkX 2.1 中),而是(节点、)对的迭代器。

Try defining degree_values using尝试使用定义degree_values

degree_values = [v for k, v in my_degrees]

Alternatively, if the order of degree_values doesn't matter, you can use或者,如果degree_values的顺序无关紧要,您可以使用

degree_values = dict(my_degrees).values()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM