2

I am using the python libraries pyVis and Networkx to visualise graphs where each node is a port somewhere in the world and each link is a route between ports. The situation is as follows: with pyVis I can tell it the positions of the nodes (x and y) but when I use the longitude and latitude of the ports it doesn't place them properly, I guess it's because I'm using coordinates in an HTML page without any processing. My question is: can I use some pyVis method to tell it that the positions are coordinates and therefore change the reference system of the HTML page or do I have to write the code to do it?

Thank you, I leave the code in case it can help you.

def create_display_attributes(attr: dict, edge: bool = False) -> dict:
    if not edge:
        return {
            'title': attr['country'],
            'group': attr['region'],
            'x': attr['pos'][0], #lon
            'y': attr['pos'][1]  #lat
        }
    else:
        return {
            'color': color_edges[attr['year']]
        }

def create_display_network(graph: nx.MultiDiGraph) -> nx.MultiDiGraph:
    nx_graph = nx.MultiDiGraph()
    nx_graph.add_nodes_from(
        [
            (node, create_display_attributes(attr))
            for node, attr in graph.nodes(data=True)
        ]
    )
    nx_graph.add_edges_from(
        [
            (source, target, create_display_attributes(attr, edge=True))
            for source, target, attr in graph.edges(data=True)
            if source != target
        ]
    )
    return nx_graph



def draw_graph(graph: nx.MultiDiGraph, title):
    nt = Network(height="100%", width="100%", directed=True, heading=title)
    nt.from_nx(create_display_network(graph))
    nt.show('nx.html')
Miguel-kjh
  • 51
  • 7

0 Answers0