[Network Graph] Set each edge with different color

Hi everyone !

I am currently working with Plotly & networkx Python library, and I am wondering if this is possible to color each edge I created between nodes to a different color. As I can see, on networkx, the code is:

G = nx.Graph()
G.add_edge(1,2,color='r',weight=2)
G.add_edge(2,3,color='b',weight=4)
G.add_edge(3,4,color='g',weight=6)

On the documentation, Github or on the Internet, I did not found any tips about. This is quite simple for a Node, I just have to provide a list of colors, is it the same for edges ? Any help or link for me ? :slight_smile:

Thank you all !

Hi Toshiro,
did u get any solution?Even I am searching for a solution

@vyshakc1992 To color edges with different colors, just append each one to the list data, as a different trace . A short example here:

edges=np.array([(2,3), (3, 1), (1, 0)], dtype=np.uint8)
nodes_loc=np.array([[0.3, 0.7], [1.2, 2.6], [2, 3.5], [0.85, 0.5]])
colors=['red', 'green', 'magenta']
nodes=dict(type='scatter',
           x=nodes_loc[:,0],
           y=nodes_loc[:,1],
           mode='markers',
           marker=dict(size=8, color='blue'))

edges_list=[ dict(type='scatter',
             x=[nodes_loc[e[0]][0], nodes_loc[e[1]][0]],
             y=[nodes_loc[e[0]][1], nodes_loc[e[1]][1]],
              mode='lines',
              line=dict(width=2, color=colors[k]))  for k, e in enumerate(edges)]
data=edges_list+[nodes]
fig=dict(data=data, layout=layout)

Thanks a lot, Empet…:slight_smile:

Thanks @empet ! I effectively did not found my answer in the past, so this will help me a lot in the future :slight_smile:

Hi
Can we do it after we create the edge_trace and node_trace? Here is my complete code.

@parthi2929 If you modify the function show_map such that it returns the Figure, then
after calling it by fig=show_map(G) you can color some edges if you
know which edge in the list of edges should have another color. Let us say you want to color the edges of index 3, 7, 18.

Then the following code defines a new (colored) edge trace
and a new figure::

idx=[3, 7, 18]

xx=[]
yy=[]

for k in idx:
    xx.extend([fig['data'][0]['x'][3*k], fig['data'][0]['x'][3*k+1], None])
    yy.extend([fig['data'][0]['y'][3*k], fig['data'][0]['y'][3*k+1], None])
    
colored_edges=dict(type='scatter',
                   mode='line',
                   line=dict(width=2, color='red'),
                   x=xx,
                   y=yy)


data1=[colored_edges]+fig['data']
fig1=dict(data=data1, layout=fig['layout'])
iplot(fig1)

Thank you very much.

Is there a way to get edge indices given we have node indices. If you notice I am passing path variable which has node labels (and using that to color appropriate node)

So internally if I could derive edge indices out of same information, I could right away color as per your snippet.

nodelist=list(G.nodes)
edgelist=list(G.edges)

Working with these two lists you can split the list of edges in two sublists corresponding to the two colors you intend to use. This is a Python problem, not a Plotly one.

Thank you. yeah if not available we should do it outside lib. I was thinking something like below could be available

edge_index = G.get_edge_index(node_index_1, node_index_2) #None if they nodes not connected

@empet Thanks a lot for those answers. I was wondering giving different colours for each edge based on their weight.

I able plot edges with different width based on their frequency and now I want to try with different colour based on their weight (in my case Frequency).

Here my code:
def make_edge(x, y, text, width):

    return  go.Scatter(x = x,
                       y = y,
                       line = dict(width = width,
                                   color = 'cornflowerblue'),
                       hoverinfo = 'text',
                       text = ([text]),
                       mode = 'lines')


# For each edge, make an edge_trace, append to list
edge_trace = []
for edge in graph.edges():

    if graph.edges()[edge]['Frequency'] > 0:
        char_1 = edge[0]
        char_2 = edge[1]

        x0, y0 = pos[char_1]
        x1, y1 = pos[char_2]

        text   = char_1 + '--' + char_2 + ': ' + str(graph.edges()[edge]['Frequency'])

        trace  = make_edge([x0, x1, None], [y0, y1, None], text,
                           0.1*graph.edges()[edge]['Frequency']**1.75)

        edge_trace.append(trace)

Frequency is my weight.