matplotlibnetworkx

Edges do not fully connect nodes when using arrows


I want to plot my graph using bended edges (connectionstyle="arc3, rad=0.3").

From the warning

The arrowstyle keyword argument is not applicable when drawing edges
with LineCollection.

To make this warning go away, either specify `arrows=True` to
force FancyArrowPatches or use the default values.
Note that using FancyArrowPatches may be slow for large graphs.

I use arrows=True resulting in the following code snippet:

import networkx as nx

G = nx.gnm_random_graph(n=1000, m=100)
fig, ax = plt.subplots(dpi=300)
nx.draw_networkx_nodes(G, pos, edgecolors='black', node_size=20, linewidths=0.25, ax=ax)
nx.draw_networkx_edges(G, pos, width=0.5, connectionstyle="arc3, rad=0.3", edge_color='gray', arrows=True, min_source_margin=0, min_target_margin=0, ax=ax)

However, the edges (bended or not) do not fully connect the nodes:

enter image description here

What am I doing wrong?


Solution

  • The issue is a mismatch in the node_size parameter. The default value in draw_networkx_edges is 300, not 20.

    Sample code output

    import matplotlib.pyplot as plt
    import networkx as nx
    
    G = nx.gnm_random_graph(n=1000, m=100)
    pos = nx.random_layout(G)
    fig, ax = plt.subplots(dpi=300)
    nx.draw_networkx_nodes(G, pos, edgecolors='black', node_size=20, linewidths=0.25, ax=ax)
    nx.draw_networkx_edges(G, pos, width=0.5, connectionstyle="arc3, rad=0.3", edge_color='gray', arrows=True, min_source_margin=0, min_target_margin=0, node_size=20, ax=ax)
    plt.show()