✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🧬 Learn how to build RNA-Seq data apps with Python & Dash. Register for the May 20 Webinar!

Changing Line Colours in PLotly Networkx Graph

Hi
Would like to supply a list of coluors to change colours of lines in Plotly Networkx Graph. With networkx it is possible to change the colours of lines by providing a list of edge colours to the graph. In my case I am creating a network correlation graph want to colour the edges by whether they are positive or negative correlations. For Networkx as follows

nx.draw(mst, with_labels=True, pos=nx.fruchterman_reingold_layout(mst),
node_size=80, node_color="#e1575c", edge_color=edge_colours, ALPHA = .8,K=8,
width=edge_width)

#Where edge_colours is a list that has a colour per edge[‘red’,‘green’,‘green’…]

For the plotly version

tracer = go.Scatter(x=Xedges, y=Yedges,
mode=‘lines’,
line=dict(color=‘grey’, width=3),
hoverinfo=‘none’,
showlegend=False)

changing colour to edge_colours gives the following.

*ValueError: *

  • Invalid value of type ‘builtins.list’ received for the ‘color’ property of scatter.line*

  •    Received value: ['#9eccb7', '#ff0000', '#9eccb7', '#9eccb7', '#9eccb7', '#ff0000', '#9eccb7', '#9eccb7', '#9eccb7', '#ff0000', '#ff0000', '#9eccb7', '#ff0000', '#ff0000', '#9eccb7', '#ff0000', '#ff0000', '#9eccb7', '#9eccb7', '#9eccb7', '#9eccb7', '#ff0000', '#ff0000', '#ff0000', '#9eccb7', '#9eccb7', '#ff0000', '#9eccb7', '#ff0000', '#ff0000', '#9eccb7', '#9eccb7', '#9eccb7', '#9eccb7', '#9eccb7', '#ff0000', '#9eccb7', '#9eccb7', '#ff0000', '#9eccb7', '#9eccb7', '#9eccb7', '#9eccb7', '#9eccb7', '#ff0000', '#9eccb7', '#9eccb7', '#ff0000', '#9eccb7', '#9eccb7', '#9eccb7']*
    
  • The ‘color’ property is a color and may be specified as:*

  •  - A hex string (e.g. '#ff0000')*
    
  •  - An rgb/rgba string (e.g. 'rgb(255,0,0)')*
    
  •  - An hsl/hsla string (e.g. 'hsl(0,100%,50%)')*
    
  •  - An hsv/hsva string (e.g. 'hsv(0,100%,100%)')*
    
  •  - A named CSS color:*
    
  •        aliceblue, antiquewhite, aqua, aquamarine, azure,*
    

Is it possible to do this in plotly and if not will it be added as a feature in the future?

All help appreciated

Rose

Hi @RosBai,

With Plotly you have to split the trace for edges in two traces: the first one is destinated to edges with positive correlation and its own line_color, while the second one for edges of negative correlation.

Thank you @empet
So is this what you mean ?

  1. Make a list of positive and negative edges
    tracerP = go.Scatter(x=XedgesP, y=YedgesP,
    mode=‘lines’,
    line=dict(color=‘green’, width=3),
    hoverinfo=‘none’,
    showlegend=False)
    tracerN = go.Scatter(x=XedgesN, y=YedgesN,
    mode=‘lines’,
    line=dict(color=‘red’, width=3),
    hoverinfo=‘none’,
    showlegend=False)

How do I then add tracer N and tracerP together for the figure?

thank you

@RosBai

You have 3 traces: node_trace, tracerP and tracerN. Define

fig = go.Figure(data=[tracerP, tracerN, node_trace])

Then you can perform some layout updates:

fig.update_layout(width=800, height=700, # adjust the plot window width and height 
                  title_text='Your title', title_x=0.5,
                  xaxis_visible=False
                  yaxis_visible=False)