Some of the algorithms in
https://networkx.lanl.gov/trac/browser/networkx/trunk/networkx/drawing/layout.py
work in arbitrary dimensions.
In particular spring_layout() is a force-directed node positioning
algorithm that will produce 3d coordinates:
In [1]: import networkx
In [2]: G=networkx.complete_graph(5)
In [3]: pos=networkx.spring_layout(G,dim=3)
In [4]: pos
Out[4]:
{0: array([ 0.82464301, 0.51411248, 0.32319794]),
1: array([ 0.52827386, 0.25559755, 0.34263581]),
2: array([ 0.50416188, 0.74054859, 0.31596981]),
3: array([ 0.71561698, 0.53219317, 0.70112578]),
4: array([ 0.34086317, 0.50437363, 0.58353033])}
I see the documentation is really sad. I've opened a ticket to improve that.
https://networkx.lanl.gov/trac/ticket/223
Aric
Yes, you need the Python package "numpy" for the layout functions.
Depending on your operating system and setup there are a few different
ways to get it. See http://scipy.org/Download for some help.
Feel free to post back here if you get stuck on how to proceed.
Aric
Use the lower() sting method (probably when adding edges).
In [1]: s="Jeff"
In [2]: s.lower()
Out[2]: 'jeff'
> Also, noticed the spring_layout does not output the edges - any way to
> tell it to output them as well. I was planning to just do a G.edges()
> dump and then resolved the location, but figured I'd ask since I was
> posting.
One approach is to consider the edge as a line segment between
the two node positions. e.g. e=(u,v), epos=(pos[u],pos[v]).
Aric
You can do it yourself:
In [23]: G=networkx.complete_graph(5)
In [24]: pos=networkx.spring_layout(G,dim=3)
In [25]: pos
Out[25]:
{0: array([ 0.23307185, 0.90192513, 0.59320213]),
1: array([ 0.32391126, 0.79692837, 0.96722863]),
2: array([ 0.63120853, 0.92457472, 0.6060652 ]),
3: array([ 0.35269727, 0.52267822, 0.5608564 ]),
4: array([ 0.63876546, 0.59418772, 0.82962413])}
In [26]: for p in pos:
pos[p]*=10.0
....:
....:
In [28]: pos
Out[28]:
{0: array([ 2.33071854, 9.01925133, 5.93202133]),
1: array([ 3.23911259, 7.96928369, 9.67228628]),
2: array([ 6.31208531, 9.24574724, 6.06065198]),
3: array([ 3.52697273, 5.22678218, 5.60856403]),
4: array([ 6.38765461, 5.94187721, 8.29624132])}
etc.
Aric
You can set the initial positions for the spring layout
In [1]: import numpy
In [2]: import networkx as nx
In [3]: G=nx.complete_graph(4)
In [4]: pos=nx.random_layout(G,dim=3) # assign random positions
In [5]: pos
Out[5]:
{0: array([ 0.288445 , 0.91503167, 0.86394004]),
1: array([ 0.04104901, 0.13421718, 0.62346402]),
2: array([ 0.38496808, 0.76948755, 0.83526977]),
3: array([ 0.19058883, 0.61016701, 0.82655111])}
In [6]: pos[0]=numpy.array([0,0,0]) # assign node 0 position
In [7]: pos
Out[7]:
{0: array([0, 0, 0]),
1: array([ 0.04104901, 0.13421718, 0.62346402]),
2: array([ 0.38496808, 0.76948755, 0.83526977]),
3: array([ 0.19058883, 0.61016701, 0.82655111])}
Then use vpos=pos in spring_layout().
Currently there is no way to fix the position of nodes during
the layout process (but that could be added to the code).
Aric