Skip to content Skip to sidebar Skip to footer

Coloring Specific Edges In Networkx

I'm running a Dikjstra's shortest path algorithm on a NetworkX Watts-Strogatz randomly generated graph and I want to color the edges of the path I've found differently from the res

Solution 1:

I found this question, python networkx - mark edges by coloring for graph drawing, which sort of answers my questions. I just needed to modify it a little bit as follows:

for e in wsGraph.edges():
    wsGraph[e[0]][e[1]]['color'] = 'grey'# Set color of edges of the shortest path to greenfor i inrange(len(path)-1):
    wsGraph[int(path[i])][int(path[i+1])]['color'] = 'red'# Store in a list to use for drawing
edge_color_list = [wsGraph[e[0]][e[1]]['color'] for e in wsGraph.edges() ]
nx.draw(wsGraph, node_color='blue', edge_color = edge_color_list,   with_labels = True)
plt.show()

I just needed to convert my path to integers instead of characters. I also changed the colours of the nodes and non-path edges to make it more clear.

Image of result:

Shortest path between 5 and 13 in a randomly generated 25-node Watts-Strogatz graph using my own Dijkstra's algorithm.

Post a Comment for "Coloring Specific Edges In Networkx"