sara
sara

Reputation: 135

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 is a spatial directed graph and my nodes therefore have specific coordinates I want to plot them at. 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). 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:

edges not connecting nodes

Here is a minimum example with 3 nodes and 2 edges that reproduces the error:

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:

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

I use nx.draw_networkx_edges and nx.draw_networkx_nodes to plot my graph. I define the layout with a dictionary of coordinates coord_dict that looks like this:

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 drawing error

I've figured out, that when not plotting the edges via arrows=False, the straight edges WILL connect. 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!

Upvotes: 4

Views: 3507

Answers (1)

yatu
yatu

Reputation: 88226

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

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)

enter image description here

Upvotes: 2

Related Questions