Search code examples
pythonpython-3.xnetworkxpyvis

Arrows Directions, 2 separate arrows both ways but not bidirectional depicted via 1 arrow


I have the following code that I've been working on:

import networkx as nx
from pyvis.network import Network
import webbrowser

# Initialize a MultiDiGraph
G = nx.MultiDiGraph()

# Add the edges and nodes
G.add_edges_from([('A', 'B'), ('B', 'A'), ('A', 'C')])

# Create a pyvis network
nt = Network(height="800px", width="1600px", notebook=True)

# Add the nodes to the network
nt.add_nodes(G.nodes())

# Add the edges to the network with arrows in both directions
for source, target in G.edges():
    nt.add_edge(source, target, arrows='to')

# Show the network
nt.show("graph.html")

# Open the HTML file in the browser
webbrowser.open("graph.html")

I am trying to generate a graph that has a edge that has an arrow going from 'A' to 'B', an arrow going from 'B' to 'A', and an arrow going from 'A' to 'C'.

I have tried using the bidirectional=True option, but this is not always the case for all the nodes, so I cannot have that. Been playing around with it to see what I can do, but I cannot seem to find what I am looking for.


Solution

  • If you set directed=True while initiating your network you should get the result you want nt = Network(height="800px", width="1600px", notebook=True,directed=True)

    enter image description here