Control layout of graph nodes in diagram

  Kiến thức lập trình

I have a dataframe containing flight connection data in a network representation

edge source target type
ZRH_ZRH_-1_base_ZRH_PMI_470_flight_idle0_start ZRH source ZRH-PMI 470 start
ZRH_PMI_470_flight_PMI_ZRH_655_flight_idle70_sit ZRH-PMI 470 PMI-ZRH 655 sit
PMI_ZRH_655_flight_ZRH_ZRH_-1_base_idle0_end PMI-ZRH 655 ZRH sink end

source or target are either a flight (e.g. ZRH-PMI 470) or a base (ZRH).
I’m trying to create the following diagram (the gray regions represent additional flight connections that are not present in the sample dataframe posted before, just to give an idea on how the final diagram should be)

enter image description here

I’m creating the network from the dataframe using

import networkx as nx

G = nx.from_pandas_edgelist(
    test,
    "source",
    "target",
    True,
     create_using=nx.DiGraph()
)

but the only way I can get kind of close to what I want is the
kamada_kawai_layout.

enter image description here

which looks weird because I don’t think is supposed to be used like this.

Is there any way to create the layout without having to manually define the positions for every node? In the end I only want to organize the connections between child and parent on horizontal lines…nothing fancy. I tried to look into the multipartite_layout but I don’t really have node attributes indicating the layers.

LEAVE A COMMENT