User123456789
User123456789

Reputation: 149

Curved edges using matplotlib and Networkx in Python 3.x

I want to draw curved edges using the Networkx framework and matplotlib.

Basically the same problem as linked below:

Networkx: Overlapping edges when visualizing MultiGraph

One answer is:

import networkx as nx
G = nx.DiGraph()
G.add_nodes_from([0,1])
pos = nx.circular_layout(G)
nx.draw_networkx_nodes(G, pos, connectionstyle='arc3, rad = 0.1', node_color = 'r', node_size = 100, alpha = 1)
nx.draw_networkx_edges(G, pos,connectionstyle='arc3, rad = 0.1', edgelist = [(0,1)], width = 2, alpha = 0.5, edge_color='b')
nx.draw_networkx_edges(G, pos,connectionstyle='arc3, rad = 0.1', edgelist= [(1,0)], width = 1, alpha = 1)
plt.axis('off')
plt.show() 

But that produces:

enter image description here

In the end I want to produce something like this:

enter image description here

Upvotes: 2

Views: 855

Answers (1)

Lee
Lee

Reputation: 31050

I don't think you can do this directly with networkx functions. But you can use matplotlib directly using the node positions you have calculated.

E.g. based on this:

import networkx as nx
G = nx.DiGraph()
G.add_nodes_from([0,1])
pos = nx.circular_layout(G)
nx.draw_networkx_nodes(G, pos, node_color = 'r', node_size = 100, alpha = 1)
ax = plt.gca()
ax.annotate("",
                xy=pos[0], xycoords='data',
                xytext=pos[1], textcoords='data',
                arrowprops=dict(arrowstyle="->", color="0.5",
                                shrinkA=5, shrinkB=5,
                                patchA=None, patchB=None,
                                connectionstyle="arc3,rad=0.3",
                                ),
                )
plt.axis('off')
plt.show()

Gives:

enter image description here

Upvotes: 3

Related Questions