Separate edge arrows in python/networkx directed graph

时光总嘲笑我的痴心妄想 提交于 2021-01-04 03:26:43

问题


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).


回答1:


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()




回答2:


You'll need a MultiDiGraph for multiple edges between two nodes:

G = nx.MultiDiGraph()

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')

To visualise the network you could use Graphviz which does display parallel edges. You could write the graph in dot and display the graph with graphviz.Source:

from networkx.drawing import nx_pydot
from graphviz import Source

nx_pydot.write_dot(G, 'multig.dot')
Source.from_file('multig.dot')



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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!