summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNavya Agarwal <82928853+navyagarwal@users.noreply.github.com>2023-04-07 06:37:04 +0530
committerGitHub <noreply@github.com>2023-04-06 18:07:04 -0700
commitd4e2331db1121c96b5fd09793b28c2e6a9cd03c2 (patch)
treef6b19e83335bacfa90822d153300abb35f147428
parent32f8a6c3c14dc9f81eaf7076e71ef58417c42d78 (diff)
downloadnetworkx-d4e2331db1121c96b5fd09793b28c2e6a9cd03c2.tar.gz
Fix handling of pos node attribute in to_agraph (#6474)
Make sure the `"pos"` node attribute is properly formatted for pygraphviz, if present.
-rw-r--r--networkx/drawing/nx_agraph.py7
-rw-r--r--networkx/drawing/tests/test_agraph.py12
2 files changed, 19 insertions, 0 deletions
diff --git a/networkx/drawing/nx_agraph.py b/networkx/drawing/nx_agraph.py
index 2ffa21fd..9a29fab5 100644
--- a/networkx/drawing/nx_agraph.py
+++ b/networkx/drawing/nx_agraph.py
@@ -137,6 +137,13 @@ def to_agraph(N):
) from err
directed = N.is_directed()
strict = nx.number_of_selfloops(N) == 0 and not N.is_multigraph()
+
+ for node in N:
+ if "pos" in N.nodes[node]:
+ N.nodes[node]["pos"] = "{},{}!".format(
+ N.nodes[node]["pos"][0], N.nodes[node]["pos"][1]
+ )
+
A = pygraphviz.AGraph(name=N.name, strict=strict, directed=directed)
# default graph attributes
diff --git a/networkx/drawing/tests/test_agraph.py b/networkx/drawing/tests/test_agraph.py
index c99d29fb..1ade719b 100644
--- a/networkx/drawing/tests/test_agraph.py
+++ b/networkx/drawing/tests/test_agraph.py
@@ -240,3 +240,15 @@ class TestAGraph:
pos = list(pos.values())
assert len(pos) == 5
assert len(pos[0]) == 3
+
+ def test_no_warnings_raised(self):
+ # Test that no warnings are raised when Networkx graph
+ # is converted to Pygraphviz graph and 'pos'
+ # attribute is given
+ G = nx.Graph()
+ G.add_node(0, pos=(0, 0))
+ G.add_node(1, pos=(1, 1))
+ A = nx.nx_agraph.to_agraph(G)
+ with pytest.warns(None) as record:
+ A.layout()
+ assert len(record) == 0