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: