Construct and label graph in NetworkX using dictionaries?

62 views
Skip to first unread message

Josh L. Espinoza

unread,
Jun 9, 2015, 5:55:48 PM6/9/15
to networkx...@googlegroups.com

How can you use node position and edge information to label a graph in NetworkX?

I like the structured nature of the hypercube_graph() instead of the randomness of the spring_layout()

For a 3 dimensional figure, the nx.Graph() works but not for a 4 dimensional figure. 

If you have a dictionary of nodes to edges:

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

and a dictionary of nodes to positions within a graph:

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)}

I want to use a DiGraph():

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

H = nx.DiGraph()

with the layout of a 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.        ])}

Is there any way to assign the layout positions of the hypercube_graph() to my nodes using the dictionaries I've created?

I tried making a dictionary of the nodes and the hypercube layout like:

D_node_layout = {}
D_pos_layout = nx.spring_layout(HC, dim= dimension)
for node,v in D_node_pos.items():
    D_node_layout[node] = D_pos_layout[v]

It still doesn't work when I try to draw it and I can't figure out why:

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

I get the following AssertionError:

  Traceback (most recent call last):
  File "hasse.py", line 56, in <module>
    nx.draw(H, pos = D_node_layout, with_labels = True,node_shape = 'o',arrows = True)
  File "/Users/Mu/anaconda/lib/python2.7/site-packages/networkx-1.9.1-py2.7.egg/networkx/drawing/nx_pylab.py", line 131, in draw
    draw_networkx(G, pos=pos, ax=ax, **kwds)
  File "/Users/Mu/anaconda/lib/python2.7/site-packages/networkx-1.9.1-py2.7.egg/networkx/drawing/nx_pylab.py", line 265, in draw_networkx
    edge_collection = draw_networkx_edges(G, pos, **kwds)
  File "/Users/Mu/anaconda/lib/python2.7/site-packages/networkx-1.9.1-py2.7.egg/networkx/drawing/nx_pylab.py", line 542, in draw_networkx_edges
    transOffset = ax.transData,
  File "/Users/Mu/anaconda/lib/python2.7/site-packages/matplotlib/collections.py", line 1016, in __init__
    self.set_segments(segments)
  File "/Users/Mu/anaconda/lib/python2.7/site-packages/matplotlib/collections.py", line 1030, in set_segments
    self._paths = [mpath.Path(seg) for seg in _segments]
  File "/Users/Mu/anaconda/lib/python2.7/site-packages/matplotlib/path.py", line 148, in __init__
    assert vertices.shape[1] == 2
AssertionError
Reply all
Reply to author
Forward
0 new messages