我知道之前曾问过类似的问题,但我的有点复杂。我试图根据它们的出度来定义networkx图的节点大小,但是会弄得一团糟。这是我的代码:
nodes = pd.read_csv('nodes_1984.csv')
edges = pd.read_csv('edges_1984.csv')
nodes.head()
this how my data containing nodes looks like (click)
network=nx.DiGraph()
for row in nodes.iterrows():
network.add_node(row[1][0], Label=row[1][1], region=row[1][2], pos=(row[1][4], row[1][3]))
for row in edges.iterrows():
network.add_edge(row[1][0],row[1][1], weight=row[1][3])
pos=nx.get_node_attributes(network,'pos')
d_out = network.out_degree() #dictionary of {nodes:out_degree_values}
d_out_val = [] #creating a list which contains values of out_degree of nodes and sorting them in the same order as they were in the d_out dict
for w in sorted(d_out.keys()):
d_out_val.append(d_out[w])
d_out_val
plt.figure(figsize=(32,28)) #plotting my network
nx.draw(network, pos, with_labels=True, node_size=[s * 100 for s in d_out_val], node_color="w")
plt.show()
结果是图中我的节点的大小不对应于这些节点的out度值,尽管我在上面对包含节点的度值的列表(d_out_val)进行了排序。 你能告诉我这个问题能解决什么吗?
答案 0 :(得分:2)
传递nx.draw
nodelist
参数,在该参数中提供已排序的节点列表。而不是
nx.draw(network, pos, with_labels=True, node_size=[s * 100 for s in d_out_val], node_color="w")
你将拥有:
nx.draw(network, pos, nodelist=sorted(network.nodes()), with_labels=True, node_size=[s * 100 for s in d_out_val], node_color="w")
如果您没有提供nodelist
参数,networkx将使用network.nodes()
(与您的node_size
列表不同,它不会被排序)。