summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Schult <dschult@colgate.edu>2016-01-06 13:55:08 -0500
committerDan Schult <dschult@colgate.edu>2016-01-06 13:55:08 -0500
commit399fec8b80bf49e2ea90126b8598863897c01570 (patch)
tree7c5066605c85626c1d7eec534591fcf959825b4f
parentc6ac93b333210d9b044a536f864a61a720bb0723 (diff)
parent32e8e0c6104b742b84016fd48a398208c1f7a8e4 (diff)
downloadnetworkx-399fec8b80bf49e2ea90126b8598863897c01570.tar.gz
Merge pull request #1900 from dschult/layout-scaling
Fix layout for single node graphs and shells
-rw-r--r--networkx/drawing/layout.py11
-rw-r--r--networkx/drawing/tests/test_layout.py8
2 files changed, 14 insertions, 5 deletions
diff --git a/networkx/drawing/layout.py b/networkx/drawing/layout.py
index 9dc217d7..77c3a8a5 100644
--- a/networkx/drawing/layout.py
+++ b/networkx/drawing/layout.py
@@ -176,13 +176,13 @@ def shell_layout(G, nlist=None, dim=2, scale=1, center=None):
if len(G) == 0:
return {}
- elif len(G) == 1:
- return {G.nodes()[0]: center}
+ if len(G) == 1:
+ return {nx.utils.arbitrary_element(G): center}
if nlist is None:
# draw the whole graph in one shell
- nlist = [list(G.nodes())]
+ nlist = [list(G)]
if len(nlist[0]) == 1:
# single node at center
@@ -587,8 +587,9 @@ def _rescale_layout(pos,scale=1):
pos[:,i]-=pos[:,i].mean()
lim=max(pos[:,i].max(),lim)
# rescale to (-scale,scale) in all directions, preserves aspect
- for i in range(pos.shape[1]):
- pos[:,i]*=scale/lim
+ if lim > 0:
+ for i in range(pos.shape[1]):
+ pos[:,i]*=scale/lim
return pos
diff --git a/networkx/drawing/tests/test_layout.py b/networkx/drawing/tests/test_layout.py
index bcd6d236..25fd138c 100644
--- a/networkx/drawing/tests/test_layout.py
+++ b/networkx/drawing/tests/test_layout.py
@@ -59,3 +59,11 @@ class TestLayout(object):
pos=nx.drawing.layout._sparse_fruchterman_reingold(A,dim=3)
assert_equal(pos.shape,(6,3))
+
+ def test_single_nodes(self):
+ G=nx.path_graph(1)
+ vpos = nx.shell_layout(G)
+ assert(vpos[0].any() == False)
+ G=nx.path_graph(3)
+ vpos = nx.shell_layout(G, [[0], [1,2]])
+ assert(vpos[0].any() == False)