首页 > 解决方案 > 从 networkx 绘制以底图位置为中心的图形

问题描述

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

标签: pythonnetworkxmatplotlib-basemappygraphviz

解决方案


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

# (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)

推荐阅读