summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Schult <dschult@colgate.edu>2016-01-01 18:14:22 -0500
committerDan Schult <dschult@colgate.edu>2016-01-01 18:14:22 -0500
commit32e8e0c6104b742b84016fd48a398208c1f7a8e4 (patch)
tree36d100d915cc44c14c9023d61eab3cf845f2992c
parent4a56f73f3ac454f7e408ea71928ff710c760a3ef (diff)
downloadnetworkx-32e8e0c6104b742b84016fd48a398208c1f7a8e4.tar.gz
Fix layout for single node graphs and shells
_rescale_layout was dividing by zero for single 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 9bbc6f98..bd4b316d 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)