Skip to content

Instantly share code, notes, and snippets.

@vappiah
Last active March 24, 2022 22:18
Show Gist options
  • Save vappiah/a253e2ae64d623c1a89cfb594ac6e596 to your computer and use it in GitHub Desktop.
Save vappiah/a253e2ae64d623c1a89cfb594ac6e596 to your computer and use it in GitHub Desktop.
import matplotlib.pyplot as plt
import numpy as np
import networkx as nx
nodes=['1', '2', '3', '4', '5']
P=nx.Graph()
P.add_nodes_from(nodes)
weighted_edges=[('1', '2' ,0.11),('1', '3', 3.1),('1','5',2.25),('4','5',0.25),('2','5',0.2),('2','4',0.22),('2','3',0.2)]
P.add_weighted_edges_from(weighted_edges)
#get edges, positions and weights
pos=nx.spring_layout(P)
labels=nx.get_edge_attributes(P,'weight')
nx.draw(P, with_labels = True)
#get edge labels
edge_labels=dict([((u,v,),d['weight']) for u,v,d in P.edges(data=True)])
#add edge labels
plot=nx.draw_networkx_edge_labels(P,pos,edge_labels=edge_labels,font_weight='normal')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment