Python 节点大小取决于 NetworkX 上的节点度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16566871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:00:45  来源:igfitidea点击:

Node size dependent on the node degree on NetworkX

pythonpython-2.7social-networkingnetworkx

提问by sunny

I imported my Facebook data onto my computer in the form of a .json file. The data is in the format:

我以 .json 文件的形式将我的 Facebook 数据导入到我的计算机上。数据格式如下:

{"nodes":[{"name":"Alan"},{"name":"Bob"}],"links":[{"source":0,"target:1"}]}

Then, I use this function:

然后,我使用这个函数:

def parse_graph(filename):
"""
Returns networkx graph object of facebook
social network in json format
"""
G = nx.Graph()
json_data=open(filename)
data = json.load(json_data)
# The nodes represent the names of the respective people
# See networkx documentation for information on add_* functions
G.add_nodes_from([n['name'] for n in data['nodes']])
G.add_edges_from([(data['nodes'][e['source']]['name'],data['nodes'][e['target']]['name']) for e in data['links']])
json_data.close()
return G

to enable this .json file to be used a graph on NetworkX. If I find the degree of the nodes, the only method I know how to use is:

启用此 .json 文件以在 NetworkX 上使用图形。如果我找到节点的度数,我知道如何使用的唯一方法是:

degree = nx.degree(p)

Where pis the graph of all my friends. Now, I want to plot the graph such that the size of the node is the same as the degree of that node. How do I do this?

其中p是我所有朋友的图表。现在,我想绘制图形,使节点的大小与该节点的度数相同。我该怎么做呢?

Using:

使用:

nx.draw(G,node_size=degree)

didn't work and I can't think of another method.

没有用,我想不出另一种方法。

采纳答案by miles82

Update for those using networkx 2.x

使用 networkx 2.x 的用户更新

The API has changed from v1.x to v2.x. networkx.degreeno longer returns a dictbut a DegreeViewObject as per the documentation.

API 已从 v1.x 更改为 v2.x。networkx.degree不再返回 a dictbut a DegreeViewObject 根据文档

There is a guide for migrating from 1.x to 2.x here.

有从1.x中迁移到2.x指导这里

In this case it basically boils down to using dict(g.degree)instead of d = nx.degree(g).

在这种情况下,它基本上归结为使用dict(g.degree)而不是d = nx.degree(g).

The updated code looks like this:

更新后的代码如下所示:

import networkx as nx
import matplotlib.pyplot as plt

g = nx.Graph()
g.add_edges_from([(1,2), (2,3), (2,4), (3,4)])

d = dict(g.degree)

nx.draw(g, nodelist=d.keys(), node_size=[v * 100 for v in d.values()])
plt.show()


nx.degree(p) returns a dict while the node_size keywod argumentneeds a scalar or an array of sizes. You can use the dict nx.degree returns like this:

nx.degree(p) 返回一个字典,而node_size keywod 参数需要一个标量或一个大小数组。您可以像这样使用 dict nx.degree 返回:

import networkx as nx
import matplotlib.pyplot as plt

g = nx.Graph()
g.add_edges_from([(1,2), (2,3), (2,4), (3,4)])

d = nx.degree(g)

nx.draw(g, nodelist=d.keys(), node_size=[v * 100 for v in d.values()])
plt.show()

enter image description here

在此处输入图片说明

回答by Tchotchke

@miles82 provided a great answer. However, if you've already added the nodes to your graph using something like G.add_nodes_from(nodes), then I found that d = nx.degree(G)may not return the degrees in the same order as your nodes.

@miles82 提供了一个很好的答案。但是,如果您已经使用类似 的方式将节点添加到图形中G.add_nodes_from(nodes),那么我发现d = nx.degree(G)可能不会以与您的节点相同的顺序返回度数。

Building off the previous answer, you can modify the solution slightly to ensure the degrees are in the correct order:

基于上一个答案,您可以稍微修改解决方案以确保度数的顺序正确:

d = nx.degree(G)
d = [(d[node]+1) * 20 for node in G.nodes()]

Note the d[node]+1, which will be sure that nodes of degree zero are added to the chart.

请注意d[node]+1,这将确保将度数为零的节点添加到图表中。