我将我的Facebook数据以.json文件的形式导入到了电脑上。这些数据的格式如下:
{
"nodes": [
{"name": "Alan"},
{"name": "Bob"}
],
"links": [
{"source": 0, "target: 1"}
]
}
然后,我使用这个函数:
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
nodes = data["nodes"]
G.add_nodes_from([n["name"] for n in nodes])
G.add_edges_from(
[
nodes[e["source"]]["name"],
nodes[e["target"]]["name"]) for e in data["links"]
]
)
json_data.close()
return G
为了使这个
.json
文件能够在 NetworkX 上用作图形,如果我要查找节点的度数,我唯一知道如何使用的方法是:degree = nx.degree(p)
这里的p是我所有朋友的图形。现在,我想绘制这个图形,使得节点的大小与该节点的度数相同。我该怎么做?
使用:
nx.draw(G, node_size=degree)
这个方法不起作用,我想不到其他的方法了。
[(v+1) * 100 for v in d.values()]
,这样零度节点就会出现。 - Tchotchke