# Change colour of line in network graph based on condition

**URL:** <https://community.plotly.com/t/change-colour-of-line-in-network-graph-based-on-condition/75324>\
**Category:** 📊 Plotly Python\
**Created:** [May 16, 2023, 1:32pm UTC](https://community.plotly.com/t/change-colour-of-line-in-network-graph-based-on-condition/75324 "2023-05-16T13:32:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![edieskga](https://avatars.discourse-cdn.com/v4/letter/e/87869e/32.png) [@edieskga](https://community.plotly.com/u/edieskga)\
**Post date:** [May 16, 2023, 1:32pm UTC](https://community.plotly.com/t/change-colour-of-line-in-network-graph-based-on-condition/75324/1 "2023-05-16T13:32:04Z")

</div>

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

```auto
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

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

```

I get this error

```auto
      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

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

```

It works no problem.

How can I solve this ?

I have already tried this alternative

```auto
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,  
🙂

---

<div class="post-metadata">

**Author:** ![AIMPED](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/aimped/32/21758_2.png) [@AIMPED](https://community.plotly.com/u/AIMPED)\
**Post date:** [May 17, 2023, 5:54am UTC](https://community.plotly.com/t/change-colour-of-line-in-network-graph-based-on-condition/75324/2 "2023-05-17T05:54:59Z")

</div>

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

> [@Change color of continuous line based on value](https://community.plotly.com/t/change-color-of-continuous-line-based-on-value/68938/2):
>
> Hi @yogi_dhiman , I think there is no built in function for this. There are two possible solutions I came up with, the first does not answer your question but could be a option, the second includes some preparation of your data first. The first is coloring the markers instead of the line: import plotly.graph\_objects as go import numpy as np # generate data x = np.arange(0, 8\*np.pi, 0.1) y = np.sin(x) # cutoff value cutoff = 0 # generate color list colors=['red' if val \< cutoff else 'blue' …
