nx.write_点(…)在输入节点有冒号时生成冗余节点

2024-05-14 05:56:16 发布

您现在位置:Python中文网/ 问答频道 /正文

import networkx as nx

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

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

生产

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

这是因为结肠。在

so.dot

^{pr2}$

注意它去掉了冒号和后面的所有东西。在

如果我手动将其更改为

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

很好。事实上,有没有节点并不重要,只要有边就行。在

如果删除:和{}之间的空格,则只生成A和B。在

我试过用各种方法摆脱结肠,但似乎不管用。显然,我每次都可以手动删除节点,但最好使用脚本化的解决方案。(而不是通过.dot文件的第二个脚本)

有人有主意吗?在


Tags: testimportnetworkx脚本add节点soas
1条回答
网友
1楼 · 发布于 2024-05-14 05:56:16

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

从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.

然而,根据this answer的说法,您可以通过向Graphviz传递一个带引号的节点名来克服这种行为,例如

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

相关问题 更多 >