在 python/networkx 有向图中分离边箭头 [英] Separate edge arrows in python/networkx directed graph

查看:134
本文介绍了在 python/networkx 有向图中分离边箭头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得类似的东西:

使用 python 库

然而,节点3和4之间的箭头是叠加的,看起来只是一个有两个头的单箭头.是否可以将它们稍微分开,以便更明显地表明那里有两条边而不是一条边?(我知道可以使用 pygraphviz 来完成,但我正在尝试使用 matplotlib 来完成).

解决方案

我前段时间创建了 networkx 绘图实用程序来解决这个问题以及我遇到的其他几个问题.该包名为

I would like to obtain something similar to this:

using the python library networkx. I can generate a similar directed graph using the following code:

import matplotlib.pyplot as plt
import networkx as nx

G = nx.DiGraph()

G.add_edge('1','2')
G.add_edge('1','3')
G.add_edge('3','2')
G.add_edge('3','4')
G.add_edge('4','3')

nx.draw(G, node_color='w', edgecolors='k', width=2.0, with_labels=True)
plt.show()

which produces:

However, the arrows between the nodes 3 and 4 are superimposed, and it just looks as a single arrow with two heads. Would it be possible to separate them slightly, in order to make more evident the fact that there are two edges over there and not just one? (I know that it can be done using pygraphviz, but I am trying to do it using matplotlib).

解决方案

I forked the networkx drawing utilities some time ago to work around this and several other issues I have had. The package is called netgraph, and supports drawing of networkx and igraph graph structures (as well as simple edge lists). It uses matplotlib under the hood, and exposes the created artists so that it easy to manipulate them further even if there is not in-built functionality to do so.

#!/usr/bin/env python

"""
https://stackoverflow.com/questions/61412323/separate-edge-arrows-in-python-networkx-directed-graph
"""

import matplotlib.pyplot as plt
import networkx as nx
import netgraph

G = nx.DiGraph()

G.add_edge('1','2')
G.add_edge('1','3')
G.add_edge('3','2')
G.add_edge('3','4')
G.add_edge('4','3')

netgraph.draw(G, node_color='w', edge_color='k', edge_width=2.0, node_labels={str(ii) : str(ii) for ii in range(1,5)})
plt.show()

这篇关于在 python/networkx 有向图中分离边箭头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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