giving the shortest path a different color than the rest in a plot

Viewed 180

i'm trying to give the shortest path a diffrent color than the rest of the edges but it doent work as all the edges come out with the same color

color_list = []
for i in G.edges():
    if i in shortestpath:  # if edges is the shortest path color with red
        color_list.append("red")
    else:  # other wise color green 
        color_list.append("Green")
print('c_list',color_list)
print(shortestpath)
#output:(3, 0, 1)
print(G.edges)
#output:[(0, 1), (0, 3), (1, 2), (2, 3), (3, 4)]

weight=nx.get_edge_attributes(G,'weight')
pos=nx.get_node_attributes(G,'pos')

nx.draw_networkx_edge_labels(G,pos,edge_labels=weight)
nx.draw(G,pos,with_labels=weight)
nx.draw_networkx_edges(G,pos,edge_color=color_list)
plt.show()

so my desired output is the path (3,0,1) to be colored differently like this : enter image description here

2 Answers

Assuming you already know the shortest path you can simply change the color of those edges by using the edgelist attribute of nx.draw_networkx_edges.

First, you turn your shortest_path tuple into a list of edges(list of tuples).

shortestpath = (3, 0, 1)
sp = []
for i in range(0,len(shortestpath)-1):
    sp.append((shortestpath[i],shortestpath[i+1]))
# sp = [(3, 0), (0, 1)]

Then paint the edges composing the shortest path in red:

nx.draw_networkx_edge_labels(G, pos, edge_labels=weight)
nx.draw(G, pos, with_labels=weight)
# painting every edge green
nx.draw_networkx_edges(G, pos, edge_color="green")
# painting specific edges red
nx.draw_networkx_edges(G, pos, edgelist=sp, edge_color="r")
plt.show()

The problem in your code is this statements:

if i in shortestpath

Here i can be a tuple, say (0,1) and you are trying to search it in (1, 0, 3) which will return false.

You basically need to check

  1. That 0 and 1 both exist in shortest path
  2. Both occur adjacent to each other

Here is the code

import networkx as nx
import matplotlib.pyplot as plt


G = nx.Graph()

G.add_edge(0, 1, weight=10)
G.add_edge(2, 1, weight=5)
G.add_edge(3, 2, weight=25)
G.add_edge(0, 3, weight=3)
G.add_edge(4, 3, weight=8)

shortestpath = nx.shortest_path(G, source=1, target=3, weight='weight')

color_list = []
for u, v in G.edges():
    # Check for both cases, i.e. (0, 1) and (1, 0) exist in shortest path
    # Refer : https://stackoverflow.com/a/8625376/8160718
    if any([u,v] == shortestpath[i:i+2] for i in range(len(shortestpath) - 1)):  
        color_list.append("red")
    elif any([v,u] == shortestpath[i:i+2] for i in range(len(shortestpath) - 1)):
        color_list.append("red")
    else:  # other wise color green 
        color_list.append("green")
print('c_list',color_list)
print(shortestpath)
#output:(3, 0, 1)
print(G.edges)
#output:[(0, 1), (0, 3), (1, 2), (2, 3), (3, 4)]

weight=nx.get_edge_attributes(G,'weight')
pos=nx.spring_layout(G)

nx.draw_networkx_edge_labels(G,pos,edge_labels=weight)
nx.draw(G,pos,with_labels=weight)
nx.draw_networkx_edges(G,pos,edge_color=color_list)
plt.show()

enter image description here

You can refer this link for more information about how the pair of elements are checked for membership and adjacency at the same time.

Here is a Google Colab Notebook with the working code.

Reference:

Related