gpt4 book ai didi

python - 根据 networkx 中的节点度数定义节点大小时结果错误

转载 作者:行者123 更新时间:2023-12-01 03:30:45 24 4
gpt4 key购买 nike

我知道以前有人问过类似的问题,但我的问题有点复杂。我试图根据网络的出度来定义网络图的节点大小,但遇到了一些困惑。这是我的代码:

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()

结果是,尽管我对包含上述节点的度值的列表 (d_out_val) 进行了排序,但图中节点的大小与这些节点的出度值并不对应。您能告诉一下这个问题可以解决什么吗?

最佳答案

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 不同,它没有排序)列表)。

关于python - 根据 networkx 中的节点度数定义节点大小时结果错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40965490/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com