Skip to content

Instantly share code, notes, and snippets.

@amazingsmash
Last active September 12, 2020 20:38
Show Gist options
  • Save amazingsmash/4a9560dd319ae775048dfd0a732fbed4 to your computer and use it in GitHub Desktop.
Save amazingsmash/4a9560dd319ae775048dfd0a732fbed4 to your computer and use it in GitHub Desktop.
Draw a networkx graph with weighted edges.
import networkx as nx
import matplotlib.pyplot as plt
def draw_weighted_graph(G, pos=None,
node_size=200,
edge_width=3,
font_size=12,
node_color='red',
color_map='Blues'):
"""Draw a networkx graph with weighted edges"""
if not pos:
pos = nx.spring_layout(G)
plt.figure(figsize=(10,10))
nx.draw_networkx_nodes(G, pos,
node_color=node_color,
node_size=node_size)
nx.draw_networkx_edges(G, pos,
width=edge_width,
edge_color=range(nx.number_of_edges(G)),
edge_cmap=plt.get_cmap(color_map),
edge_vmin=None,
edge_vmax=None)
nx.draw_networkx_labels(G,
pos,
font_size=font_size)
nx.draw_networkx_edge_labels(G, pos,
edge_labels={e:G[e[0]][e[1]]['weight'] for e in G.edges})
plt.axis('off')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment