问题描述
这是我先前在here上发布的问题的后续操作,以可视化图形中的边缘攻击。
以下代码已发布为answer to my previous post
import matplotlib.pyplot as plt
import networkx as nx
H = nx.gnm_random_graph(n=8,m=9,seed=5) # generate a random graph
H.add_edges_from([('I',1),(5,'O')]) # adding input/output nodes
pos = nx.spring_layout(H,iterations=200) # find good positions for nodes
def attack(G,edge,color):
G.remove_edge(*edge) # first remove the edge
# check if another could be also impacted
if G.degree[edge[0]] == 1:
neighbor = [n for n in G.neighbors(edge[0])][0]
impacted_edge = (edge[0],neighbor)
elif G.degree[edge[1]] == 1:
neighbor = [n for n in G.neighbors(edge[1])][0]
impacted_edge = (edge[1],neighbor)
else:
impacted_edge = None
G.add_edge(*edge) # put back the edge to redraw it
# redraw the attacked edge (solid) and the possible impacted one (dashed)
if impacted_edge:
nx.draw_networkx_edges(
G,edgelist=[impacted_edge],pos=pos,edge_color=color,style='dashed',width=4
)
nx.draw_networkx_edges(
G,edgelist=[edge],label=f'attack {edge[0]}{edge[1]}',style='solid',width=4
)
# attack some edges
attack(H,(6,4),color='red')
attack(H,(3,6),color='blue')
attack(H,(1,2),color='green')
nx.draw(H,pos,node_size=700,with_labels=True,node_color='gray')
plt.legend()
plt.show()
实线表示受到攻击的边缘,相同颜色的虚线表示由于特定攻击而受到影响的相邻边缘。
答案是有帮助的,但是当受影响的边缘重叠时会出现问题。
示例
attack(H,color='red')
attack(H,color='yellow')
颜色重叠,很难看到。如果我们能画出虚线来表示受影响/受攻击的边缘彼此相邻,而不会像this image所示那样重叠,那将是很好的。
有关如何改善此可视化的建议将不胜感激!
编辑:下面发布的答案对于2D网络确实有用,我仍然
寻找方法来扩展它以可视化pyvis中的3D网络(即x,y,z坐标可用作节点的pos
属性)。建议将不胜感激。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)