Drawing a large weighted network in networkx based on thickness?

丶灬走出姿态 提交于 2019-12-04 11:34:53

You can specify each edge individually, or you can define them in groups if you have some function to compute groupings (and then use multiple calls to draw_network_edges).

Here's an example with a random graph that uses the edge weights as is, first to define the edge thickness and second, using the data as coloring instead.

import matplotlib.pyplot as plt
import networkx as nx
import numpy as np

n = 15; m = 40

# select some edge destinations
L = np.random.choice(xrange(n), 2*m)
# and suppose that each edge has a weight
weights = 0.5 + 5 * np.random.rand(m)

# create a graph object, add n nodes to it, and the edges
G = nx.DiGraph()
G.add_nodes_from(xrange(n))
for i, (fr, to) in enumerate(zip(L[1::2], L[::2])):
  G.add_edge(fr, to, weight=weights[i])

# use one of the edge properties to control line thickness
edgewidth = [ d['weight'] for (u,v,d) in G.edges(data=True)]

# layout
pos = nx.spring_layout(G, iterations=50)
#pos = nx.random_layout(G)

# rendering
plt.figure(1)
plt.subplot(211); plt.axis('off')
nx.draw_networkx_nodes(G, pos)
nx.draw_networkx_edges(G, pos, width=edgewidth,)

plt.subplot(212); plt.axis('off')

# rendering
nx.draw_networkx_nodes(G, pos)
nx.draw_networkx_edges(G, pos, edge_color=edgewidth)

plt.show()

Which gives you something like this:

Clearly you could use a more complicated function to assemble the list of edgewidth values as suitable to your application (e.g. binned values or a product of different properties) as well.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!