Search code examples
pythonnetworkxgraph-visualizationpyvis

Plotting multi-edge directed graphs using pyvis in a way that shows all edges separately


Note: This post is directly related to this one, except that I am looking for a solution in pyvis.

I would like to plot a directed graph with multiedges, that is, each pair of nodes is connected with two edges in opposite directions. In order to that, it is important, that the two edges do not overlap visually. When using networkx.draw() this problem can be solved by passing connectionstyle='arc3, rad = 0.1' to nx.draw() as described here. However, I am using for a solution in pyvis.

Example using networkx.draw():

import networkx
from pyvis.network import Network

# create a graph
G = nx.MultiDiGraph()
G.add_edges_from([
    (1, 2),
    (2, 3),
    (3, 2),
    (2, 1),
])


# draw with networkx using the proposed solution
nx.draw(G, connectionstyle='arc3, rad = 0.1')

enter image description here

# This is how the graph currently looks with pyvis
nt = Network('500px', '500px',directed=True)
nt.from_nx(G)
nt.show('nx.html')

enter image description here


Solution

  • What version are you using? I just tried running your code:

    import networkx as nx
    from pyvis.network import Network
    
    # create a graph
    G = nx.MultiDiGraph()
    G.add_edges_from([
        (1, 2),
        (2, 3),
        (3, 2),
        (2, 1),
    ])
    
    # This is how the graph currently looks with pyvis
    nt = Network('500px', '500px',directed=True)
    nt.from_nx(G)
    nt.show('nx.html')
    

    and I obtain the result you are looking for:

    two nodes with arc edges

    You can find the output HTML here: https://pastecode.io/s/anav9333

    I am using version pyvis=0.3.0