Change colour of line in network graph based on condition

Hey there Iā€™m building a plot with the networkx library, and I need to get the edges a specific color if a certain condition is filled for the moment I have managed to generate the list of colors if want

edge_colors = []
for edge in G.edges():
    if check_specific_part(edge):
        edge_colors.append('red')
    else:
        edge_colors.append('blue')

The list works and has the same number of values as the number of edges. But now when i want to pass it as an argument for the color like so

`edge_trace = go.Scatter(
    x=[],
    y=[],
    hoverinfo='none',
    mode='lines',
    line=dict(width=0.5, color=edge_colors),
    )
`

I get this error

      9         edge_colors.append('blue')
     10 print(edge_colors)
---> 13 edge_trace = go.Scatter(
     14     x=[],
     15     y=[],
     16     hoverinfo='none',
     17     mode='lines',
     18     line=dict(width=0.5, color=edge_colors),
     19     )
     21 node_colors = []
     22 for node in G.nodes():

File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\plotly\graph_objs\_scatter.py:3331, in Scatter.__init__(self, arg, alignmentgroup, cliponaxis, connectgaps, customdata, customdatasrc, dx, dy, error_x, error_y, fill, fillcolor, fillpattern, groupnorm, hoverinfo, hoverinfosrc, hoverlabel, hoveron, hovertemplate, hovertemplatesrc, hovertext, hovertextsrc, ids, idssrc, legendgroup, legendgrouptitle, legendrank, legendwidth, line, marker, meta, metasrc, mode, name, offsetgroup, opacity, orientation, selected, selectedpoints, showlegend, stackgaps, stackgroup, stream, text, textfont, textposition, textpositionsrc, textsrc, texttemplate, texttemplatesrc, uid, uirevision, unselected, visible, x, x0, xaxis, xcalendar, xhoverformat, xperiod, xperiod0, xperiodalignment, xsrc, y, y0, yaxis, ycalendar, yhoverformat, yperiod, yperiod0, yperiodalignment, ysrc, **kwargs)
   3329 _v = line if line is not None else _v
   3330 if _v is not None:
-> 3331     self["line"] = _v
   3332 _v = arg.pop("marker", None)
   3333 _v = marker if marker is not None else _v

File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\plotly\basedatatypes.py:4842, in BasePlotlyType.__setitem__(self, prop, value)
   4840 # ### Handle compound property ###
   4841 if isinstance(validator, CompoundValidator):
...

When i write one color like this

line=dict(width=0.5, color='black')

It works no problem.

How can I solve this ?

I have already tried this alternative

for edge in G.edges():
    x0, y0 = pos[edge[0]]
    x1, y1 = pos[edge[1]]
    edge_trace['x'] += tuple([x0, x1, None])
    edge_trace['y'] += tuple([y0, y1, None])
    edge_colors['line']['color'] += 'blue' if check_specific_part(edge) else 'red'

And it still doesnt work
How can i make this work ?

Thank you in advance for your help,
:slight_smile:

Hi @edieskga, the scatter only allows asingle value as the line_color argument. You could add segmented lines like in this example: