How can I draw undirected graph?

I have a list of nodes and edges,
for example:

nodes = [0,1,2,3,4,5]
edges = [(0,1),(0,1), (0,4),(1,2),(2,3),(1,3),(3,0),(0,2),(4,5)]

also, I have a label per each node

colors = {0: ‘color1’, 1: ‘color2’, 2: ‘color3’, 3: ‘color4’, 4: ‘color5’}

which I would like to use as a color of nodes.

how to draw such a graph?

There seem to be more Python/Graphviz folks on stackoverflow.

I asked here because could’t find a clear explanation on stackoverflow or anywhere… but well, I will try stackoverflow again

Is this actually a Python/Graphviz question? I’m not really sure of the context, but I would attempt something like:

mygraph = io.StringIO()
mygraph.write("digraph {\n")
for n in nodes:
  mygraph.write(f'  n{n} [color="{colors[n]}"];\n')
for begin, end in edges:
  mygraph.write(f"  n{begin} -> n{end};\n")
mygraph.write("}\n")
print(mygraph.getvalue())

yes it is Python/Graphviz question
and currently it works only for small graphs and without color
for huge graphs I receive SIGKILL error but I need to find the solution exactly for huge graphs

from graphviz import Graph
import pandas as pd

df = pd.DataFrame({'ChildID':[1, 1, 2, 3], 'ParentID':[2, 3, 3, 2]})
g = Graph('processs', filename='process.gv')
# run over all the rows and for each row add a new edge to the graph
for index, row in df.iterrows():
    g.edge(str(row['ChildID']), str(row['ParentID']))
g.view()

Is this about pygraphviz? I’m not sure what the Python package graphviz you’re importing is, as this doesn’t look like the SWIG bindings.

it is this one graphviz (official one i guess (?))

Ah, a different one again. If anything can said to be the official one, it’s probably the in-tree bindings. Though I don’t think any mainstream packaging system actually ships these. So most Python users interact with Graphviz through one of the third party modules mentioned in this thread.

If you want to see if it’s an issue with how graphviz · PyPI is using memory, you could try the method I suggested to construct input and then feed it directly to Graphviz. But there’s always going to be some size limit on what Graphviz can handle determined by the available memory in your computer.

1 Like

okay thank you Im going to try your suggestion then!
do you by any chance know any other python packages that can manage to draw huge graphs ?

Roughly, how big is huge? Approximately how many nodes & edges?

This one isn’t Graphviz based, but networkx tries to hit this niche.

it’s 227912 nodes and 3593554 edges and it might become even bigger

i tried this one, it works fine with smaller graphs but on huge graph it sends SIGKILL

That is a very large graph. And ~20 edges/node (each edge touches 2 nodes) will truly make for a (virtual) printer explosion.
I am not an expert in very large graph, but if it were mine:

  • start with the sfdp engine (sfdp | Graphviz) It seems to be best suited for very large graphs
  • separate Python & Graphviz - use Python to create a text Graphviz input file then separately run sfdp (or other engines) on that text input file.
  • use the -v command line option (Command Line | Graphviz) to get debugging/logging info
  • look here (https://graphviz.org/doc/info/attrs.html_) for attributes that might speed-up or improve your sfdp runs.
1 Like

oh okay, thank you! looks a little complicated (I thought there were some easier options :sweat_smile:) , but I will try this

You could also try t-SNE or UMAP to embed the points in 2D space. Really it is not realistic\ to render millions of nodes and edges on a conventional display. How many pixels do you have? What if you wanted to view a text document with millions of words, would you expect to show that on a conventional screen? What’s the goal of this?

Stephen North

it’s not necessarily in pixels, it can be for example in html format (so with zooming in it will be a closer look at exact nodes and their edges)
the goal is to create the best visual representation of such huge graph

thank you! I will check t-SNE and UMAP