获取随机生成的图中的节点位置
Get nodes positions in a randomly generated graph
我已经使用 NetworkX 库生成了一个随机图。现在,我想得到每个节点的 (x,y) 坐标。
我试过什么?
import sys
import matplotlib.pyplot as plt
import networkx as nx
import random as random
from random import randint
import numpy as np
from itertools import chain
G = nx.balanced_tree(3,3)
node_positions = nx.spring_layout(G)
我得到了什么?
node_positions
Out[56]:
{0: array([ 0.50797246, 0.47719004]),
1: array([ 0.4737295 , 0.80685253]),
2: array([ 0.61668036, 0.5967835 ]),
3: array([ 0.45314193, 0.18113052]),
4: array([ 0.74582 , 0.88535594]),
5: array([ 0.28304618, 0.73784419]),
6: array([ 0.22814727, 0.91634686]),
7: array([ 0.90767018, 0.55866571]),
8: array([ 0.22321579, 0.57663404]),
9: array([ 0.82803591, 0.73905281]),
10: array([ 0.15093966, 0.24096575]),
11: array([ 0.41389402, 0.01848409]),
12: array([ 0.74298423, 0.10585789]),
13: array([ 0.95692361, 0.70135218]),
14: array([ 0.9333499 , 0.77529735]),
15: array([ 0.59008687, 0.99666832]),
16: array([ 0.40042269, 0.97903855]),
17: array([ 0.12273361, 0.54150101]),
18: array([ 0.21783263, 0.46879425]),
19: array([ 0.1477097 , 0.86215834]),
20: array([ 0.31065064, 0.9689898 ]),
21: array([ 0.0734333 , 0.75194536]),
22: array([ 0.97066494, 0.34137316]),
23: array([ 0.8496917 , 0.86787867]),
24: array([ 1. , 0.43286285]),
25: array([ 0. , 0.48385456]),
26: array([ 0.12788817, 0.75838036]),
27: array([ 0.0137882 , 0.39184635]),
28: array([ 0.99627758, 0.57863309]),
29: array([ 0.89164448, 0.82786694]),
30: array([ 0.71802896, 0.9607354 ]),
31: array([ 0.18064846, 0.12315356]),
32: array([ 0.00104408, 0.53985017]),
33: array([ 0.0779511 , 0.23378722]),
34: array([ 0.58219827, 0.01923722]),
35: array([ 0.30061834, 0.04107663]),
36: array([ 0.49707994, 0. ]),
37: array([ 0.69939259, 0.04731438]),
38: array([ 0.84674553, 0.13220172]),
39: array([ 0.87227266, 0.21870199])}
我在找什么?
(x,y) 空间坐标,例如 {0: array([ 15.5, 38])} 这样 x_coord 是 15.5 而 y_coord 是 38.
但是,我看不懂{0: array([ 0.50797246, 0.47719004])}
这是否意味着 x_coord=0.50797246
和 y_coord=0.47719004
?
如果是这种情况,则意味着 grahes 的所有节点都在同一个节点中,因为这里的最大值是 1!!
我正在寻找 skimage 中的整数坐标:
segments_slic = slic(img, n_segments=150, compactness=0.01, sigma=1)
segments_slic=segments_slic +1
regions = regionprops(segments_slic)
如果我们想得到坐标(x,y)我们只需要regions[0].coords
然后我们得到:
regions[0].coords
array([[ 0, 0],
[ 0, 1],
[ 0, 2],
...,
[27, 46],
[27, 47],
[27, 48]])
检查 nx.spring_layout
文档 here,查找 scale
和 k
参数:
scale (number (default: 1)) – Scale factor for positions. Not used unless fixed is None.
k (float (default=None)) – Optimal distance between nodes. If None the distance is set to 1/sqrt(n) where n is the number of nodes. Increase this value to move nodes farther apart.
关于获取整数坐标,您可以使用 int
轻松完成。
positions = { node: (int(pos[0]),int(pos[1])) for node,pos in node_positions.items() }
[编辑]
k
是Fruchterman-Reingold布局算法的参数,是节点间排斥强度的一种。
我最好展示一下它的作用(用不同的 k 布局的同一张图):
k=0.001
k=0.1
我已经使用 NetworkX 库生成了一个随机图。现在,我想得到每个节点的 (x,y) 坐标。
我试过什么?
import sys
import matplotlib.pyplot as plt
import networkx as nx
import random as random
from random import randint
import numpy as np
from itertools import chain
G = nx.balanced_tree(3,3)
node_positions = nx.spring_layout(G)
我得到了什么?
node_positions
Out[56]:
{0: array([ 0.50797246, 0.47719004]),
1: array([ 0.4737295 , 0.80685253]),
2: array([ 0.61668036, 0.5967835 ]),
3: array([ 0.45314193, 0.18113052]),
4: array([ 0.74582 , 0.88535594]),
5: array([ 0.28304618, 0.73784419]),
6: array([ 0.22814727, 0.91634686]),
7: array([ 0.90767018, 0.55866571]),
8: array([ 0.22321579, 0.57663404]),
9: array([ 0.82803591, 0.73905281]),
10: array([ 0.15093966, 0.24096575]),
11: array([ 0.41389402, 0.01848409]),
12: array([ 0.74298423, 0.10585789]),
13: array([ 0.95692361, 0.70135218]),
14: array([ 0.9333499 , 0.77529735]),
15: array([ 0.59008687, 0.99666832]),
16: array([ 0.40042269, 0.97903855]),
17: array([ 0.12273361, 0.54150101]),
18: array([ 0.21783263, 0.46879425]),
19: array([ 0.1477097 , 0.86215834]),
20: array([ 0.31065064, 0.9689898 ]),
21: array([ 0.0734333 , 0.75194536]),
22: array([ 0.97066494, 0.34137316]),
23: array([ 0.8496917 , 0.86787867]),
24: array([ 1. , 0.43286285]),
25: array([ 0. , 0.48385456]),
26: array([ 0.12788817, 0.75838036]),
27: array([ 0.0137882 , 0.39184635]),
28: array([ 0.99627758, 0.57863309]),
29: array([ 0.89164448, 0.82786694]),
30: array([ 0.71802896, 0.9607354 ]),
31: array([ 0.18064846, 0.12315356]),
32: array([ 0.00104408, 0.53985017]),
33: array([ 0.0779511 , 0.23378722]),
34: array([ 0.58219827, 0.01923722]),
35: array([ 0.30061834, 0.04107663]),
36: array([ 0.49707994, 0. ]),
37: array([ 0.69939259, 0.04731438]),
38: array([ 0.84674553, 0.13220172]),
39: array([ 0.87227266, 0.21870199])}
我在找什么?
(x,y) 空间坐标,例如 {0: array([ 15.5, 38])} 这样 x_coord 是 15.5 而 y_coord 是 38.
但是,我看不懂{0: array([ 0.50797246, 0.47719004])}
这是否意味着 x_coord=0.50797246
和 y_coord=0.47719004
?
如果是这种情况,则意味着 grahes 的所有节点都在同一个节点中,因为这里的最大值是 1!!
我正在寻找 skimage 中的整数坐标:
segments_slic = slic(img, n_segments=150, compactness=0.01, sigma=1)
segments_slic=segments_slic +1
regions = regionprops(segments_slic)
如果我们想得到坐标(x,y)我们只需要regions[0].coords
然后我们得到:
regions[0].coords
array([[ 0, 0],
[ 0, 1],
[ 0, 2],
...,
[27, 46],
[27, 47],
[27, 48]])
检查 nx.spring_layout
文档 here,查找 scale
和 k
参数:
scale (number (default: 1)) – Scale factor for positions. Not used unless fixed is None.
k (float (default=None)) – Optimal distance between nodes. If None the distance is set to 1/sqrt(n) where n is the number of nodes. Increase this value to move nodes farther apart.
关于获取整数坐标,您可以使用 int
轻松完成。
positions = { node: (int(pos[0]),int(pos[1])) for node,pos in node_positions.items() }
[编辑]
k
是Fruchterman-Reingold布局算法的参数,是节点间排斥强度的一种。
我最好展示一下它的作用(用不同的 k 布局的同一张图):
k=0.001
k=0.1