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
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()
you should add a set() path_edges = set(path_edges)
after your zip() to get the shortest path coloration work
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With