Highlighting the shortest path in a Networkx graph
networkx, python-2.7
Solution
import matplotlib.pyplot as plt
G = nx.karate_club_graph()
pos = nx.spring_layout(G)
nx.draw(G,pos,node_color='k')
# draw path in red
path = nx.shortest_path(G,source=14,target=16)
path_edges = list(zip(path,path[1:]))
nx.draw_networkx_nodes(G,pos,nodelist=path,node_color='r')
nx.draw_networkx_edges(G,pos,edgelist=path_edges,edge_color='r',width=10)
plt.axis('equal')
plt.show()
Problem
I have a network of people. I can display how they are connected by creating a directed graph using Networkx. Here is a code sample: ``` edges = edglist nodes = nodelist dg.add_weighted_edges_from(edges) #print dg.nodes() print nx.shortest_path(dg, source='Freda', target='Levi', weight=None) nx.draw(dg) plt.savefig("path.png") ``` Which produces: I can also calculate a shortest path between two nodes. However what I am stuck on is how to highlight this 'shortest path'. Any pointers would be greatly appreciated. BTW, I am a newbie