使用词典在NetworkX中构建和标记统一图表?

时间:2015-06-09 19:24:33

标签: python dictionary graph network-programming networkx

如何使用节点位置和边缘信息在NetworkX中标记图表?

我喜欢hypercube_graph()的结构化特性,而不是spring_layout()的随机性。我知道NetworkX只在2D中绘图,但hypercube_graph()在2D中投影N维对象,我想将此布局与我自己的标签一起使用。 enter image description here

对于3维图形,nx.Graph()是均匀的(非随机的),但不是4维图形。我使用3D作为高维立方体的简单版本。

目前,我有边缘节点字典

D_node_edge = {'pr': ['pqr'], 
               'pq': ['pqr'], 
               'p': ['pq', 'pr'], 
               'qr': ['pqr'], 
               'q': ['pq', 'qr'], 
               '0': ['p', 'q', 'r'], 
               'pqr': [], 
               'r': ['pr', 'qr']}

和图表中的节点到字段的字典:

D_node_pos = {'pr': (1, 0, 1), 
              'qr': (0, 1, 1), 
              'p': (1, 0, 0), 
              'pq': (1, 1, 0), 
              'q': (0, 1, 0), 
              '0': (0, 0, 0), 
              'pqr': (1, 1, 1), 
              'r': (0, 0, 1)}

我想使用 DiGraph()

axis_labels = ['p','q','r']
dimension = len(axis_labels) #3 in this case

H = nx.DiGraph()

使用hypercube_graph的布局

HC = nx.hypercube_graph(dimension) #dimension = 3

>>>nx.spring_layout(HC, dim = dimension)

{(1, 1, 0): array([ 0.69543835,  0.37689489,  0.48037435]), 
 (0, 1, 1): array([ 0.27055875,  0.        ,  0.36329725]), 
 (1, 0, 0): array([ 0.41149329,  0.76917215,  0.60675544]), 
 (0, 0, 1): array([ 0.        ,  0.43156976,  0.51814906]), 
 (1, 0, 1): array([ 0.33865646,  0.53760327,  0.16742205]), 
 (0, 0, 0): array([ 0.04858308,  0.73062358,  1.        ]), 
 (0, 1, 0): array([ 0.34010671,  0.27698965,  0.81225166]), 
 (1, 1, 1): array([ 0.65369567,  0.13626658,  0.        ])}

有没有办法使用我创建的词典将hypercube_graph()的布局位置分配给我的节点?

当我尝试绘制它时它不起作用:

nx.draw(H, pos = D_node_pos, with_labels = True, node_shape = 'o', arrows = True)

我理解“pos”字典需要是keys = node和values = positions(例如'p':array([0.41149329,0.77617215,0.60675544]))但它仍然不起作用:(

...
      File "/Users/Mu/anaconda/lib/python2.7/site-packages/matplotlib/path.py", line 148, in __init__
        assert vertices.shape[1] == 2
    AssertionError

0 个答案:

没有答案