您是正确的,igraph、networkx 和 plotly 默认情况下对两条双向边使用相同的路径,这样一条边会遮挡另一条边。这里的关键词是“默认”。如果您绘制曲线边而不是直边,则双向边使用单独的路径。
如果您确实想要直边,则必须使用netgraph,它为双向边使用单独的路径。就个人而言,我更喜欢使用颜色而不是宽度来可视化边缘权重,但netgraph 当然支持使用任何一种(或两者)。
import matplotlib.pyplot as plt
import networkx as nx
import netgraph # pip install netgraph
cube = nx.cubical_graph().to_directed()
cube_with_weights = [(v1, v2, np.random.rand()) for (v1, v2) in cube.edges()]
edge_width = {(v1, v2) : 2 * w for (v1, v2, w) in cube_with_weights}
node_size = {v : 5 * np.random.rand() for v in cube.nodes()}
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(6, 3))
g1 = netgraph.Graph(cube_with_weights,
node_size = node_size,
edge_width = 2,
edge_cmap = 'RdGy',
arrows = True,
ax = ax1
)
g2 = netgraph.Graph(cube_with_weights,
node_layout = g1.node_positions,
node_size = node_size,
edge_color = 'black',
edge_width = edge_width,
arrows = True,
ax = ax2
)
plt.show()
当涉及到动态 UI 和过滤操作时,无论您使用哪个库,您都必须自己实现这些。但是,对于 igraph、networkx 和 plotly,没有(简单的)方法可以在绘制后索引、更改或删除艺术家,因此您必须在每次更改后重新绘制完整的图表。
netgraph 以易于访问的方式公开 matplotlib 艺术家。因此,在过滤操作之后更改或隐藏艺术家非常简单。例如:
node_of_interest = 0
g1.node_artists[node_of_interest].set_visible(False)
fig.canvas.draw()
edge_of_interest = (1, 2)
g1.edge_artists[edge_of_interest].set_facecolor('cyan')
fig.canvas.draw()
免责声明:我写这篇文章是因为我对网络可视化的状态感到沮丧。所以我的观点肯定是有偏见的。