繁体   English   中英

如何在我的 networkx 图中显示链接的状态?

[英]How can i show the status of the links in my networkx graph?

我正在使用 networkx 库创建一个图形,使用邻接列表作为入口文件。 这些类型的列表每行、源和目标仅允许 2 个值(任意多个目标)。

我正在过滤条目文件的创建以仅添加 UP 边缘。

- before filtering :
OK node1 node10
OK node10 node99
KO node20 node99

- after filtering :
node1 node10
node10 node99

当我在看我的图表时,我看不到一个节点是否从前一个节点消失了,因为我有很多。

例如,是否可以解析我的边缘状态并显示带有虚线的红色节点作为边缘?

是否可以将我的图表与理论图表进行比较,并用不同的颜色显示缺失的链接?

可能有更快的方法,但这是我的逻辑:

G - 最终图(干净,更少的边)
T - 理论图(原始,更多边)

首先计算理论图和最终图的边缘之间的差异:

diff = T.edges() - G.edges()

然后 plot 节点和边缘在这个差异内,就像你假装的那样:

if e in diff:
    for n in e:
        if n not in G:
            res.add_node(n, color="red")
        else:
            res.add_node(n, color='#1f78b4') # default networkx color
    res.add_edge(*e, style='--')

正常绘制不在此差异范围内的节点和边:

else:
    res.add_node(e[0], color = '#1f78b4') # default networkx color
    res.add_node(e[1], color = '#1f78b4')
    res.add_edge(*e, style='-')

最后找到节点的colors和边的styles,绘制:

colors = [u[1] for u in res.nodes(data="color")]
styles = [res[u][v]['style'] for u,v in res.edges()]
nx.draw(res, with_labels =True, node_color = colors, style = styles)

完整代码

def draw_theorethical(G,T):
    diff = T.edges() - G.edges()
    # print("Extra Edges in T", diff, "\nExtra Nodes in T", T.nodes() - G.nodes())
    res = nx.Graph()
    for e in T.edges():
        if e in diff:
            for n in e:
                if n not in G:
                    res.add_node(n, color="red")
                else:
                    res.add_node(n, color='#1f78b4') # Node invisible... default networkx color
            res.add_edge(*e, style='--')
        else:
            res.add_node(e[0], color = '#1f78b4')
            res.add_node(e[1], color = '#1f78b4')
            res.add_edge(*e, style='-')
    
    colors = [u[1] for u in res.nodes(data="color")]
    styles = [res[u][v]['style'] for u,v in res.edges()]

    nx.draw(res, with_labels =True, node_color = colors, style = styles)

例子:

对于两个图 G 和 T:

T = nx.Graph()
T.add_edge("A","B")
T.add_edge("A","C")
T.add_edge("B","D")
T.add_edge("B","F")
T.add_edge("C","E")


G = nx.Graph(T)
G.remove_edge("C","E")
G.remove_node("E")
draw_theorethical(G,T)

绘制: 结果

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM