Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
278 views
in Technique[技术] by (71.8m points)

python - How can i show the status of the links in my networkx graph?

I am creating a graph with the networkx library, using an adjacency list as an entry file. These kinds of lists allow only 2 values per line, source, and destination (as many destinations as you want).

I am filtering the creation of the entry file to add only the UP edge.

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

- after filtering :
node1 node10
node10 node99

When I am watching my graphs, I can't see if a node disappeared from the previous one, because I have a lot of them.

Is it possible to parse the status of my edges and display red nodes with a dotted line as an edge for example?

Is it possible to compare my graph to a theoretical one and display the missing link with a different color?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There might be a faster way but here is my logic:

G - final graph(clean, fewer edges)
T - theoretical graph(raw, more edges)

First calculate the difference between edges of the theoretical and final graphs:

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

Then plot nodes and edges that are within this difference like you pretended:

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='--')

Nodes and edges that aren't within this difference are drawn normally:

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

Finally, find the colors of the nodes and the styles of the edges and draw:

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)

full code

def draw_theorethical(G,T):
    diff = T.edges() - G.edges()
    # print("Extra Edges in T", diff, "
Extra 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)

example:

For two graphs, G and 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)

draws: result


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...