Hello everybody,
I have some trouble making a network graph directed. This should be possible through the parameters ‘directed=True’. However, in my example below, adding ‘directed=True’ creates double the amount of arrows, which all lead nowhere, whereas without ‘directed=True’ the undirected graph is correctly displayed.
This is a saved png of undirected version (“Undirected.html” in the code below)
and this the directed version (“Directed.html” in the code below) .
If you have any ideas why this is or know where I went wrong, please let me know. Any help would be appreciated.
import numpy as np
import pandas as pd
import holoviews as hv
import networkx as nx
from holoviews import opts
from bokeh.models import HoverTool
import os
hv.extension('bokeh')
def nx_graph():
# create an example networkx graph with 4 nodes and edges connecting consecutive nodes
G = nx.Graph()
edge_list = [(1,2), (2,3), (3,4)]
for u,v in edge_list:
G.add_edge(u, v)
return G
def nx_to_hv(nx_ptn_graph):
# positions for the nodes
pos = {1:[0,0], 2:[150, 0], 3:[250, 0], 4:[300, 0]}
# convert to holoviews graph
hv_loads_graph = hv.Graph.from_networkx(nx_ptn_graph, positions=pos)
return hv_loads_graph
def main():
# create a network x graph
G = nx_graph()
# convert it to a holoviews graph
undirected = nx_to_hv(nx_ptn_graph=G)
# save the undirected graph
hv.save(obj=undirected, filename="Undirected.html")
# make the hv graph directed
directed = undirected.opts(inspection_policy='nodes', arrowhead_length=0.05, directed=True)
# save the result
hv.save(obj=directed, filename="Directed.html")
return
if __name__ == '__main__':
main()