从以底图位置为中心的 networkx 绘制图形

Draw a graph from networkx centered on a basemap position

我正在寻找在地图上绘制多个子图,每个子图将以一个地理位置(或图的一个坐标)为中心。节点本身没有位置(或者都属于一个城市),但是每个子图都对应一个本地的情况。

通过分别计算节点的位置,我可以很好地绘制网络。您可以使用此片段代码代替您的相关部分再试一次。

# (other code above this line)
#
import numpy as np
# compute the positions here
# proper scaling (500000) is applied to the original positions ..
# .. obtained from xxx_layout() to get good spreading
pos1 = nx.spring_layout(G1)
for ea in pos1.keys():
    pos1[ea] =  np.array([mx1, my1]) + pos1[ea]*500000

pos2 = nx.circular_layout(G2)
for ea in pos2.keys():
    pos2[ea] =  np.array([mx2, my2]) + pos2[ea]*500000

nx.draw_networkx(G1, pos=pos1, node_size=100, node_color='green')
nx.draw_networkx(G2, pos=pos2, node_size=100, node_color='red')
#
# (more code below this line)

输出图:

编辑

替代版本:

import numpy as np
# compute the positions here
# proper scaling (500000) is applied to the original positions ..
# .. obtained from xxx_layout() to get good spreading

pos1 = nx.spring_layout(G1, scale=500000, center=[mx1, my1])
pos2 = nx.circular_layout(G2, scale=500000, center=[mx2, my2])

nx.draw_networkx(G1, pos=pos1, node_size=100, node_color='green')
nx.draw_networkx(G2, pos=pos2, node_size=100, node_color='red')
#
# (more code below this line)