简体   繁体   English

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

[英]Why does nx.draw_networkx_edges not connect the nodes when plotting graph edges?

I have a graph G that I want to plot.我有一个要绘制的图G G is a spatial directed graph and my nodes therefore have specific coordinates I want to plot them at. G是一个空间有向图,因此我的节点具有我想要绘制它们的特定坐标。 The graph is built from a morphological skeleton of an image using sknw.build_sknw from here (but I assume that it doesn't play a significant role).该图是使用此处的sknw.build_sknw从图像的形态骨架构建的(但我认为它没有发挥重要作用)。 However, when plotting, I am encountering some edge-connectivity problems where some edges between two neighboring nodes do not connect in the plot.但是,在绘图时,我遇到了一些边连接问题,其中两个相邻节点之间的某些边在图中没有连接。 They point in the right direction but are too short.它们指向正确的方向,但太短了。 However, this only happens with some edges.然而,这只发生在一些边上。 Most are visualized correctly:大多数都正确可视化:

不连接节点的边

Here is a minimum example with 3 nodes and 2 edges that reproduces the error:以下是重现错误的 3 个节点和 2 个边的最小示例:

I am reading in the Graph from an edgelist via:我正在通过以下方式从边缘列表中读取图表:

G = nx.read_edgelist('test.edgelist', data=True, create_using=nx.DiGraph())

test.edgelist looks as follows: 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}

The list of 'pts' corresponds to the pixels of the original morphological skeleton (in white in the first image) that are between two intersections (aka nodes from my graph). 'pts' 列表对应于原始形态骨架(第一张图像中的白色)的像素,它们位于两个交叉点(也就是我的图中的节点)之间。

I use nx.draw_networkx_edges and nx.draw_networkx_nodes to plot my graph.我使用nx.draw_networkx_edgesnx.draw_networkx_nodes来绘制我的图形。 I define the layout with a dictionary of coordinates coord_dict that looks like this:我使用坐标字典coord_dict定义布局,如下所示:

coord_dict = {'2789': [707, 261], '2823': [714, 311], '2762': [695, 259]}

Now, whenever I plot my graph with the following code:现在,每当我使用以下代码绘制图形时:

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')

I get a plot like this, where the edges do not connect to the second node:我得到一个这样的图,其中边不连接到第二个节点: networkx 绘图错误

I've figured out, that when not plotting the edges via arrows=False , the straight edges WILL connect.我发现,当不通过arrows=False绘制边缘时,直边将连接。 However, the arrows are important for my visualisations and I would therefore appreciate any help on overcoming this plotting issue.但是,箭头对我的可视化很重要,因此我将不胜感激任何有关克服此绘图问题的帮助。 Plus, I would like to understand the general issue on why some arrows will connect correctly, while others don't.另外,我想了解为什么某些箭头可以正确连接而其他箭头不能正确连接的一般问题。 Thanks!谢谢!

This is happening because the edges and nodes are being generated independently, and manually setting sizes and widths can lead to having having nodes slightly separated from the edges.发生这种情况是因为边和节点是独立生成的,手动设置大小和宽度可能会导致节点与边稍微分开。 If you don't need to draw them separately, just do it via a single call to nx.draw :如果您不需要单独绘制它们,只需通过一次调用nx.draw即可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')

在此处输入图片说明

Alternatively, same as you where doing but avoid customising the node and arrow sizes:或者,与您所做的相同,但避免自定义节点和箭头大小:

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)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM