Diffferent line widths within the same (network) graph

Hi! I’ve just gotten started with Plotly and I’m trying to plot a NetworkX graph with weighted edges. I’d like the edges to look thicker or thinner depending on their weight. I started from this example and tried to extend the code by analogy:

edge_trace = Scatter(

for edge in G.edges(data=True):
    x0, y0 = G.node[edge[0]]['pos']
    x1, y1 = G.node[edge[1]]['pos']
    edge_trace['x'] += [x0, x1, None]
    edge_trace['y'] += [y0, y1, None]

The weight values do get into edge_trace[‘line’][‘width’] but when I actually plot the graph all edges are the same width. Can anyone tell me why this doesn’t work and what I should do instead? Thanks!

Scatter line width only supports scalar values at the moment.

You can subscribe to https://github.com/plotly/plotly.js/issues/147 for the latest development info.

Not sure this is relevant but I found this when I had the same problem so posting a solution. If instead of defining a single plot for your edges, you make one per edge then you can vary the line width for each:

def make_edge(x, y, width):
        x: a tuple of the x from and to, in the form: tuple([x0, x1, None])
        y: a tuple of the y from and to, in the form: tuple([y0, y1, None])
        width: The width of the line

        a Scatter plot which represents a line between the two points given. 
    return  go.Scatter(

Run this for each node and save to a list you can then just use it as figure data. It replaces the edge_trace bit in plotly’s example network graph.

1 Like