python - 为什么在绘制图形边缘时 nx.draw_networkx_edges 不连接节点?

标签 python plot networkx

我有图 G我想绘制。 G是一个空间有向图,因此我的节点具有我想要绘制它们的特定坐标。该图是使用 sknw.build_sknw 从图像的形态骨架构建的。来自 here (但我认为它没有发挥重要作用)。
但是,在绘图时,我遇到了一些边连接问题,其中两个相邻节点之间的某些边在图中没有连接。它们指向正确的方向,但太短了。然而,这只发生在一些边上。大多数都正确可视化:
edges not connecting nodes
以下是重现错误的最小示例,其中包含 3 个节点和 2 个边:
我正在通过以下方式从边缘列表中读取图表:

G = nx.read_edgelist('test.edgelist', data=True, create_using=nx.DiGraph())
test.edgelist如下所示:
2789 2762 {'pts': [[697, 259], [698, 259], [699, 259], [700, 259], [701, 259], [702, 259], [703, 259], [704, 259], [705, 259]], 'weight': 8.0}
2789 2823 {'pts': [[708, 264], [707, 265], [706, 266], [706, 267], [706, 268], [706, 269], [705, 270], [704, 271], [703, 272], [702, 273], [701, 274], [700, 275], [699, 275], [698, 275], [697, 275], [696, 275], [695, 276], [695, 277], [695, 278], [695, 279], [695, 280], [695, 281], [695, 282], [696, 283], [697, 284], [697, 285], [697, 286], [697, 287], [698, 288], [698, 289], [699, 290], [699, 291], [699, 292], [699, 293], [700, 294], [700, 295], [701, 296], [701, 297], [701, 298], [702, 299], [702, 300], [703, 301], [704, 302], [705, 303], [706, 304], [707, 305], [707, 306], [708, 307], [708, 308], [709, 309], [710, 309], [711, 310], [712, 310], [713, 310]], 'weight': 62.94112549695427}
'pts' 列表对应于原始形态骨架(第一张图像中的白色)的像素,它们位于两个交叉点(也就是我的图中的节点)之间。
我用 nx.draw_networkx_edgesnx.draw_networkx_nodes绘制我的图表。我用坐标字典 coord_dict 定义布局看起来像这样:
coord_dict = {'2789': [707, 261], '2823': [714, 311], '2762': [695, 259]}
现在,每当我使用以下代码绘制图形时:
edges = nx.draw_networkx_edges(G, pos=coord_dict, arrows=True, arrowstyle='->', edge_color='green', arrowsize=0.75, width=0.45, ax=ax)
nodes = nx.draw_networkx_nodes(G, pos=coord_dict, node_size=0.15, node_color='red')
我得到这样的图,其中边不连接到第二个节点:
networkx drawing error
我发现,当不通过 arrows=False 绘制边缘时,直边将连接。但是,箭头对我的可视化很重要,因此我将不胜感激任何有关克服此绘图问题的帮助。另外,我想了解为什么有些箭头可以正确连接而其他箭头不能正确连接的一般问题。谢谢!

最佳答案

发生这种情况是因为边和节点是独立生成的,手动设置大小和宽度可能会导致节点与边稍微分开。如果您不需要单独绘制它们,只需调用一次 nx.draw 即可完成。 :

nx.draw(G, pos=coord_dict, 
        arrowstyle='->', 
        arrowsize=20, 
        width=2,
        with_labels=True, 
        node_size=500, 
        node_color='orange',
        edge_color='green')
enter image description here
或者,与您所做的相同,但要避免自定义节点和箭头大小:
plt.figure(figsize=(8,5))
ax = plt.gca()
nx.draw_networkx_edges(G, pos=coord_dict, arrows=True, arrowstyle='->', 
                       edge_color='green', width=2, ax=ax)
nx.draw_networkx_nodes(G, pos=coord_dict, node_color='orange')
nx.draw_networkx_labels(G, pos=coord_dict)
plt.box(False)
enter image description here

关于python - 为什么在绘制图形边缘时 nx.draw_networkx_edges 不连接节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64385340/

相关文章:

python - anaconda3 的一半有 py27 构建路径

python - Django:以表单保存多对多

python - Python中的抽象类,添加有实现和没有实现的方法是错误的吗

python - 生成具有一定程度分布的图?

python - Networkx Python 边缘比较

Python NetworkX——根据属性选项的数量自动设置节点颜色

Python datetime.strptime 默认

r - 绘图区域颜色是否有 `par()` 设置?

python - 在 for 循环中绘图,在 matplotlib 中具有 'hold on' 效果?

python - 使用 pandas 的绘图方法在 1 行中绘制图表时出现问题