简体   繁体   中英

Separate edge arrows in python/networkx directed graph

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

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

在此处输入图像描述

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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM