Create a graph in Python (with pygraphviz) when the number of nodes is huge

Question:

I am trying, in Python, to create a graph from lists where I can see the merge and ramifications of my lists. I am sure the first item of the list is always the same for all lists.

For these three example lists:

list1 = ["w", "a", "b", "c", "d"]
list2 = ["w", "a", "f", "c", "d"]
list3 = ["w", "a", "e", "f", "d"]

The result should be:

enter image description here

Reading online I found pygraphviz and I am trying to use to do it by following the documentation.

import pygraphviz as pgv
from datetime import datetime, timezone

def flatten(l): return flatten(l[0]) + (flatten(l[1:]) if len(l) > 1 else []) if type(l) is list else [l]

def nodes_to_edges(nodes):
    edges = []
    for i in range(len(nodes)-1):
        edges.append((nodes[i], nodes[i+1]))
    return edges

now = datetime.now(timezone.utc)

list1=  ["w", "a", "b", "c", "d"]
list2 = ["w", "a", "f", "c", "d"]
list3=  ["w", "a", "e", "f", "d"]
edges_list1 = nodes_to_edges(list1)
edges_list2 =  nodes_to_edges(list2)
edges_list3 =  nodes_to_edges(list3)
nodes = flatten(list1 +list2+list3)
edges = flatten(edges_list1 +edges_list2+edges_list3)

# create a new graph
G = pgv.AGraph(label=f'<<i>Generated {now.strftime("%b %d %Y %H:%M:%S")}</i>>',fontsize = 10, graph_type='digraph', directed=True)


for node in nodes:
    G.add_node(node)

for edge in edges:
    G.add_edge(edge[0], edge[1])

    
G.layout()
G.draw("file.png")

Moreover I cannot find a way to be like in my drawing, one node in the top and the others below, to see the ramifications. The image I have as result is a bit messy (see below).

enter image description here

Even worst, if is a big list one node ends in front of another and the plot is not legible.

Asked By: Catarina Nogueira

||

Answers:

By changing the line G.layout() to G.layout(prog="dot")

I got the following result with the small graph which matches your drawing.

Top down graph

Let me know if this works for your large dataset.

Answered By: user2654302
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.