2013-02-24 85 views
0

我目前有一个三元组形式(node1,node2,weight_of_edge)的列表。有什么方法让我绘制,以便它们之间有边缘的节点在布局中保持靠得很近?networkx布局

+0

我不知道在matplotlib这样的功能。有非常强大的网络可视化工具可以提供此功能。例如,请看[Cytoscape](http://www.cytoscape.org/)。 – 2013-02-25 07:33:24

回答

1

您应该看看NetworkX库,它提供了许多创建和操作网络的工具。

基于三元组列表上的一个基本的例子:

list_of_triplets = [("n1", "n2", 4), 
        ("n3", "n4", 1), 
        ("n5", "n6", 2), 
        ("n7", "n8", 4), 
        ("n1", "n7", 4), 
        ("n2", "n8", 4), 
        ("n8", "n9", 6), 
        ("n4", "n9", 12), 
        ("n4", "n6", 1), 
        ("n2", "n7", 4), 
        ("n1", "n8", 4)] 

# The line below in the code change the list in a format that take 
# a weight argument in a dictionary, to be computed by NetworkX 

formatted_list = [(node[0], node[1], {"weight":node[2]}) for node in list_of_triplets] 

要绘制图表:

import matplotlib.pyplot as plt 
import networkx as nx 

G = nx.Graph() 
G.add_edges_from(formatted_list) 
nx.draw(G) 
plt.show() 

nodes