您可以单独指定每条边,或者如果您有一些计算分组的函数(然后对draw_network_edges 进行多次调用),也可以将它们分组定义。
这是一个随机图示例,它按原样使用边权重,首先定义边粗细,然后使用数据作为着色。
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()
这会给你这样的东西:
显然,您也可以使用更复杂的函数来组装适合您的应用程序的边缘宽度值列表(例如,分箱值或不同属性的乘积)。