nx.write_dot(...) 当输入节点有冒号时生成冗余节点

nx.write_dot(...) generates redundant nodes when input nodes have a colon

import networkx as nx

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

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

产生

这是冒号的缘故。

so.dot:

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

注意它去掉了冒号及其后面的所有内容。

如果我手动将其更改为

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

没关系。其实有没有节点无所谓,只要有边就可以了。

如果我去掉:t之间的space,只会生成A和B。

我试过以各种方式转义冒号,但似乎不起作用。显然,我每次都可以手动删除节点,但最好使用脚本解决方案。 (而不是通过 .dot 文件的第二个脚本)

有人有想法吗?

这不是错误,而是 GraphViz Dot 语言语法的一个特性。节点名称中的冒号用于指定输入或输出端口。

来自 GraphViz 文档,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.

但是,根据 ,您 可以 通过向 Graphviz 传递一个带引号的节点名称来覆盖此行为,例如

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