简体   繁体   中英

Change edge thickness based on weight

When using draw_networkx_edges() of networkx, is it possible to change the thickness of the edges based on their weight?

Here I am assuming weights are less than some known number, say, k.

You can use edges(data='weight') :

Edges are returned as tuples with optional data in the order (node, neighbor, data).

That is, you can get an edge's data by edge[2] when specifying data='weight' (or edge[2]['weight'] if calling data=True to get all data elements as a dict).

Here's an example:

import networkx as nx
g = nx.Graph()
g.add_edge('A', 'B', weight=5)
g.add_edge('A', 'C', weight=15)
g.add_edge('B', 'C', weight=7)

pos = nx.spring_layout(g, seed=0)
nx.draw_networkx(g, pos)

for edge in g.edges(data='weight'):
    nx.draw_networkx_edges(g, pos, edgelist=[edge], width=edge[2])

图形绘制

One way to do is to categorize edges as shown in this example .

step 1: add weights to edges
step 2: categorize edgelist based on weight
step 3: pass edgelist to draw_networkx_edges

I stumbled on this from a different SO thread .

old broken link: http://networkx.lanl.gov/examples/drawing/weighted_graph.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM