2016-12-01 112 views
-1

我具有边缘的列表:使用权重来绘制图形与NetworkX

[[0,0,0], [0,1,1], [0,2,1], [2,3,2], ....[n,m,t]] 

其中索引0是一个节点,在列表中的索引1是一个节点,并且索引2是权重值。

我想要做的是这样的:

``` 
    0 
/\ 
    1 2 All values of weights of 1 
     \ 
     3 all values of weight of 2 

``` 

方向并不重要,它只是更容易在编辑器垂直画。 我想用matplotlib导出。

谢谢!

回答

1

您提交的边缘列表是否代表您的所有数据?如果是这样,你甚至不需要权重来绘制你想要的图像(给出你的例子)。

在下面的代码中,我使用graphviz_layout来计算图形/树的布局。请注意,代码是为Python 2编写的。同样,我仅使用边缘信息而不考虑权重。

import networkx as nx 
import matplotlib.pyplot as plt 

data = [[0,0,0], [0,1,1], [0,2,1], [2,3,2]] 
G = nx.Graph() 

for row in data: 
    G.add_edge(row[0], row[1]) 

pos = nx.graphviz_layout(G, prog='dot') # compute tree layout 
nx.draw(G, pos, with_labels=True, node_size=900, node_color='w') # draw tree and show node names 
plt.show() # show image 

输出:

enter image description here