节点大小取决于度数
Node size depending on degrees
我正在尝试将节点大小设置为等于节点的度数。
我的数据集是
Person1 Age Person2 Wedding
0 Adam John 3 Yao Ming Green
1 Mary Abbey 5 Adam Lebron Green
2 Samuel Bradley 24 Mary Abbey Orange
3 Lucas Barney 12 Julie Lime Yellow
4 Christopher Rice 0.9 Matt Red Green
我构建网络的代码是
pos=nx.spring_layout(G, k=0.20, iterations=30)
nx.draw_networkx_nodes(G, pos, node_size = degrees, nodelist=collist['value'], node_color=collist['Wedding'])
nx.draw_networkx_edges(G, pos, width = [I['Age'] for i in dict(G.edges).values()])
我试着定义度数如下
degrees=[]
for x in df['Person1']: # all nodes size should depend on the degree, so also for Person2. Maybe this step is wrong
deg=G.degree[x]
degrees.append(deg)
不过好像不是标量
错误是
ValueError: s must be a scalar, or the same size as x and y
编辑:我忘了举个例子 collist['value']
:
Wedding variable value
0 Green Person1 Adam John
1 Green Person1 Mary Abbey
... ... ... ...
75 Green Person2 Yao Ming
76 Green Person2 Adam Lebron
假设G
中的节点是从Person1
和Person2
列中获取的,df['Person1']
中的“人”数与总人数不同G 中的节点,或至少在 nodelist=collist['value']
.
中
一个简单的解决方法是考虑 G
中每个节点的度数。基本上你是对的,这一步错了:
for x in df['Person1']:
您可以将其更改为:
for x in G.nodes():
或 使用list comprehension:
degrees = [G.degree[node] for node in G.nodes()]
或者如果您只想要 collist['value']
中的节点:
degrees = [G.degree[node] for node in collist['value']]
小例子:
G = nx.barabasi_albert_graph(100, 2, seed=42)
degrees = [G.degree[node] for node in G.nodes()]
pos=nx.spring_layout(G)
nx.draw_networkx_nodes(G, pos, node_size = degrees)
nx.draw_networkx_edges(G, pos, alpha=0.1)
结果:
我正在尝试将节点大小设置为等于节点的度数。
我的数据集是
Person1 Age Person2 Wedding
0 Adam John 3 Yao Ming Green
1 Mary Abbey 5 Adam Lebron Green
2 Samuel Bradley 24 Mary Abbey Orange
3 Lucas Barney 12 Julie Lime Yellow
4 Christopher Rice 0.9 Matt Red Green
我构建网络的代码是
pos=nx.spring_layout(G, k=0.20, iterations=30)
nx.draw_networkx_nodes(G, pos, node_size = degrees, nodelist=collist['value'], node_color=collist['Wedding'])
nx.draw_networkx_edges(G, pos, width = [I['Age'] for i in dict(G.edges).values()])
我试着定义度数如下
degrees=[]
for x in df['Person1']: # all nodes size should depend on the degree, so also for Person2. Maybe this step is wrong
deg=G.degree[x]
degrees.append(deg)
不过好像不是标量
错误是
ValueError: s must be a scalar, or the same size as x and y
编辑:我忘了举个例子 collist['value']
:
Wedding variable value
0 Green Person1 Adam John
1 Green Person1 Mary Abbey
... ... ... ...
75 Green Person2 Yao Ming
76 Green Person2 Adam Lebron
假设G
中的节点是从Person1
和Person2
列中获取的,df['Person1']
中的“人”数与总人数不同G 中的节点,或至少在 nodelist=collist['value']
.
一个简单的解决方法是考虑 G
中每个节点的度数。基本上你是对的,这一步错了:
for x in df['Person1']:
您可以将其更改为:
for x in G.nodes():
或 使用list comprehension:
degrees = [G.degree[node] for node in G.nodes()]
或者如果您只想要 collist['value']
中的节点:
degrees = [G.degree[node] for node in collist['value']]
小例子:
G = nx.barabasi_albert_graph(100, 2, seed=42)
degrees = [G.degree[node] for node in G.nodes()]
pos=nx.spring_layout(G)
nx.draw_networkx_nodes(G, pos, node_size = degrees)
nx.draw_networkx_edges(G, pos, alpha=0.1)
结果: