gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 14:15:29 26 4
gpt4 key购买 nike

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:

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

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

如果我手动将其更改为

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

没关系。其实有没有节点并不重要,只要有边就行。

如果我删除 :t 之间的空格,则只会生成 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.

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

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

关于python - nx.write_dot(...) 当输入节点有冒号时生成冗余节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33722809/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com