NetworkX 不显示同一节点的边的边标签

NetworkX does not show edge label for edge to the same node

我发现 NetworkX 绘图库有一个奇怪的行为:如果边向后指向同一节点,它不会绘制边标签。

我很确定这是 NetworkX 的一项功能,因为我使用相同的方法创建了所有边缘标签,并且它可以很好地处理非循环边缘,如图所示。

是否有任何显示选项可以帮助显示所有边缘标签?

import networkx as nx
import matplotlib.pyplot as plt

g = nx.DiGraph()

edge_labels = dict()

g.add_edge(0, 1 )
#ATTENTION - this will work
edge_labels[(0, 1)] = '01'

g.add_edge(0, 0)

#ATTENTION - this line will not work
edge_labels[(0, 0)] = '00'

pos = nx.spring_layout(g)
nx.draw(g, pos, with_labels=True, font_weight='bold')
nx.draw_networkx_edge_labels(g, pos, edge_labels=edge_labels, 
font_color='red')
plt.show()

已解决 通过将 '_____' 放在循环边的字符串之前,您可以看到它。 不漂亮,但显示

        if state_id == self.last_parent.id:#if it is loop
        self.edge_labels[(self.last_parent.id, state_id)] = '_____'+ char_of_edge# add to the edge label the symbols

示例:

给定两个节点的坐标 (x1, y1)(x2, y2) 标签位置计算为

(x, y) = (
            x1 * label_pos + x2 * (1.0 - label_pos),
            y1 * label_pos + y2 * (1.0 - label_pos),
        )

其中 label_pos 的默认值为 0.5 [source]
这意味着自边缘的边缘标签与节点位置重叠,这就是为什么你看不到它,除非增加字体大小。