简体   繁体   中英

draw parallel edges using networkx

Below is a very simple example

import networkx as nx
import matplotlib.pyplot as plt

g = nx.DiGraph()
g.add_nodes_from([1,2,3])
g.add_edge(1,2, weight = 1)
g.add_edge(1,3, weight = 1)
g.add_edge(2,1, weight = 2)
nx.draw(g,with_labels=True)
plt.draw()
plt.show()

I would like to add the weight label to the edge and show parallel edges for 1,2 and 2,1 how do I do this. I am using jupyter notebook.

many thanks!

Here is a method to add weight labels to the edges:

pos=nx.spring_layout(g)
nx.draw_networkx_nodes(g, pos)
nx.draw_networkx_labels(g, pos)
nx.draw_networkx_edges(g,pos)
nx.draw_networkx_edge_labels(g,pos,edge_labels={x:g[x[0]][x[1]]['weight'] for x in g.edges})
plt.axis('off')
plt.show()

Output:

图形

Unfortunately, I don't think that networkx (or more precisely: matplotlib) is able to handle parallel edges drawing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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