3
import networkx as nx

G = nx.DiGraph()
G.add_edge("A: test", 'B: test')

nx.write_dot(G,'so.dot')

Produces

http://oi67.tinypic.com/2hrzrx3.jpg

This is due to the colon.

so.dot:

strict digraph G {
A;
B;
"A: test" -> "B: test";
}

Notice it strips the colon and everything behind it.

If I manually change this to

strict digraph G {
"A: test";
"B: test";
"A: test" -> "B: test";
}

it's fine. In fact it doesn't matter if there are nodes, as long as there are edges.

If I remove the space between : and t, only A and B are generated.

I've tried escaping the colon in various ways, but that doesn't seem to work. I can manually delete the nodes every time, obviously, but a scripted solution would be preferable. (And not a second script that goes through the .dot file)

Anyone have an idea?

svdc
  • 154
  • 2
  • 10

1 Answers1

6

This isn't a bug, it's a feature of the GraphViz Dot language syntax. The colon in a node name is used to specify input or output ports.

From the GraphViz docs, Node, Edge and Graph Attributes

portPos

Modifier indicating where on a node an edge should be aimed. It has the form portname(:compass_point)? or compass_point. If the first form is used, the corresponding node must either have record shape with one of its fields having the given portname, or have an HTML-like label, one of whose components has a PORT attribute set to portname.

However, according to this answer you can over-ride this behaviour by passing Graphviz a quoted node name, eg,

G.add_edge("'A: test'", "'B: test'")
Community
  • 1
  • 1
PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
  • Perfect. Out of curiosity, how did you find that answer? What search terms? – svdc Nov 15 '15 at 18:08
  • 1
    @svdc: It was a multi-stage process. :) FWIW, even though I'm not a Graphviz / DOT expert, I have been using it off & on for over 10 years (before I started using Python) so I've had a bit of practice searching for Graphviz info. But I must admit that I didn't know about ports before, or maybe I'd just forgotten about them. :) I've never used any of the Python Graphviz modules, my (admittedly simple) programs just create the DOT code directly. – PM 2Ring Nov 15 '15 at 18:20