Last active
September 12, 2020 20:38
-
-
Save amazingsmash/4a9560dd319ae775048dfd0a732fbed4 to your computer and use it in GitHub Desktop.
Draw a networkx graph with weighted edges.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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