如何解决在3D模式下可视化链接攻击的最佳方法是什么
这是我先前在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
属性)。建议将不胜感激。
解决方法
避免图形中边缘重叠的另一种好方法是使用pyvis
,但是这里的问题是它仅支持有向图中的平行边缘。一种解决方案是将图形可视化为有向图,然后调整边缘选项以使箭头不明显。在上述代码的末尾添加以下代码段。
from pyvis.network import Network
N = Network(bgcolor='#222222',font_color='white',directed=True)
N.set_edge_smooth('dynamic')
for n in H.nodes:
N.add_node(n,color='gray')
for e in H.edges:
N.add_edge(e[0],e[1],color='gray',width=0)
for e in impacted_edges:
N.add_edge(e[0],color=e[2],width=1,dashes=True)
for e in edges:
N.add_edge(e[0],width=1)
N.set_options('''
var options = {
"edges": {
"arrows": {
"to": {
"enabled": true,"scaleFactor": 0
},"middle": {
"enabled": true,"from": {
"enabled": true,"scaleFactor": 0
}
},"color": {
"inherit": true
},"smooth": {
"forceDirection": "none"
}
},"physics": {
"minVelocity": 0.75
}
}
''')
N.write_html('example_graph.html')
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。