# [Network Graph] Set each edge with different color

#1

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()
``````

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 ?

Thank you all !

#2

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

#3

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

#4

Thanks a lot, Empetâ€¦

#5

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

#6

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

#7

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

#8

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.

#9
``````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.

#10

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