如何在有向图中的两个节点之间制作弯曲边? [英] How can I make curved edges between two nodes in a DiGraph?

查看:40
本文介绍了如何在有向图中的两个节点之间制作弯曲边?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 NetworkX 库在有向图中的两个节点之间添加弯曲箭头.

函数的文档

您必须将边列表转换为方形邻接矩阵,其中连接的缺失由 NaN 表示,非 NaN 条目被解释为边权重并映射到边颜色.然后,您可以使用

调用该模块

network_line_graph.draw(adjacency_matrix, node_order=None, arc_above=True)

请注意,如果您没有明确指定节点顺序,则会使用递归最小流切割优化节点顺序,以将强连接的子网/节点放在一起(理想情况下,您会最小化总弧长,但计算成本会很快).

API与networkx非常相似,但是如果确实有任何问题,请在github上提出问题.

I am trying to add curved arrows between two nodes in a DiGraph by using NetworkX library.

The documentation for the function nx.draw_networkx_edges mentions the argument connectionstyle. Setting connectionstyle='arc3, rad = 0.2' should yield curved connectors. But I am unable to reproduce this. I have also looked into the module matplotlib.patches and call ConnectionStyle as mentioned in https://matplotlib.org/stable/api/_as_gen/matplotlib.patches.ConnectionStyle.html ; but I still get the same visual.

array_of_edges = list(zip(df.start_node, df.end_node, df.weight))
G.add_weighted_edges_from(array_of_edges)
nx.draw_networkx_edges(
    G, pos_nodes,
    edge_color='#5B174C',
    width=df['weight'] / 600,
    alpha=0.8,
    connectionstyle='arc3,rad=0.2')

This is what I am getting and this is what I require: https://imgur.com/a/1TcPfY9

解决方案

I had a similar issue a while back (I wanted to compare two states of the same network). I couldn't work out a solution using networkx so I made my own, the code for which can be found here.

You will have to convert your edge list into a square adjacency matrix, where the absence of a connection is denoted by a NaN, and non-NaN entries are interpreted as edge weights and mapped to edge colors. You can then call the module using

network_line_graph.draw(adjacency_matrix, node_order=None, arc_above=True) 

Note that if you don't specify the node order explicitly, the node order is optimised using recursive minimum flow cuts to place strongly connected subnetworks/nodes together (ideally you would minimize total arc length but that gets computationally expensive very quickly).

The API is pretty similar to networkx but if you do have any problems, please raise an issue on the github.

这篇关于如何在有向图中的两个节点之间制作弯曲边?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆