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 ?

@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)

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.

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.

@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)