# How can I draw undirected graph?

**URL:** <https://forum.graphviz.org/t/how-can-i-draw-undirected-graph/1069>\
**Category:** Help\
**Created:** [March 11, 2022, 9:49pm UTC](https://forum.graphviz.org/t/how-can-i-draw-undirected-graph/1069 "2022-03-11T21:49:51Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Asya](https://avatars.discourse-cdn.com/v4/letter/a/22d042/32.png) [@Asya](https://forum.graphviz.org/u/Asya)\
**Post date:** [March 11, 2022, 9:49pm UTC](https://forum.graphviz.org/t/how-can-i-draw-undirected-graph/1069/1 "2022-03-11T21:49:51Z")

</div>

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

```auto
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?

---

<div class="post-metadata">

**Author:** ![steveroush](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@steveroush](https://forum.graphviz.org/u/steveroush)\
**Post date:** [March 12, 2022, 7:44pm UTC](https://forum.graphviz.org/t/how-can-i-draw-undirected-graph/1069/2 "2022-03-12T19:44:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Asya](https://avatars.discourse-cdn.com/v4/letter/a/22d042/32.png) [@Asya](https://forum.graphviz.org/u/Asya)\
**Post date:** [March 13, 2022, 7:45pm UTC](https://forum.graphviz.org/t/how-can-i-draw-undirected-graph/1069/3 "2022-03-13T19:45:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![smattr](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/smattr/32/85_2.png) [@smattr](https://forum.graphviz.org/u/smattr)\
**Post date:** [March 14, 2022, 2:47am UTC](https://forum.graphviz.org/t/how-can-i-draw-undirected-graph/1069/4 "2022-03-14T02:47:12Z")

</div>

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

```python
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())

```

---

<div class="post-metadata">

**Author:** ![Asya](https://avatars.discourse-cdn.com/v4/letter/a/22d042/32.png) [@Asya](https://forum.graphviz.org/u/Asya)\
**Post date:** [March 14, 2022, 8:00am UTC](https://forum.graphviz.org/t/how-can-i-draw-undirected-graph/1069/5 "2022-03-14T08:00:17Z")

</div>

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

```auto
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()

```

---

<div class="post-metadata">

**Author:** ![smattr](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/smattr/32/85_2.png) [@smattr](https://forum.graphviz.org/u/smattr)\
**Post date:** [March 14, 2022, 2:57pm UTC](https://forum.graphviz.org/t/how-can-i-draw-undirected-graph/1069/6 "2022-03-14T14:57:22Z")

</div>

Is this about [pygraphviz](https://pypi.org/project/pygraphviz/)? I’m not sure what the Python package `graphviz` you’re importing is, as this doesn’t look like the SWIG bindings.

---

<div class="post-metadata">

**Author:** ![Asya](https://avatars.discourse-cdn.com/v4/letter/a/22d042/32.png) [@Asya](https://forum.graphviz.org/u/Asya)\
**Post date:** [March 14, 2022, 3:50pm UTC](https://forum.graphviz.org/t/how-can-i-draw-undirected-graph/1069/7 "2022-03-14T15:50:21Z")

</div>

it is this one [graphviz](https://pypi.org/project/graphviz/) (official one i guess (?))

---

<div class="post-metadata">

**Author:** ![smattr](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/smattr/32/85_2.png) [@smattr](https://forum.graphviz.org/u/smattr)\
**Post date:** [March 15, 2022, 2:30am UTC](https://forum.graphviz.org/t/how-can-i-draw-undirected-graph/1069/8 "2022-03-15T02:30:32Z")

</div>

Ah, a different one again. If anything can said to be the official one, it’s probably the [in-tree bindings](https://gitlab.com/graphviz/graphviz/-/blob/691665c854528bc8502cec119023a06a17b45fb6/tclpkg/gv/gv.i). 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](https://pypi.org/project/graphviz/) 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.

---

<div class="post-metadata">

**Author:** ![Asya](https://avatars.discourse-cdn.com/v4/letter/a/22d042/32.png) [@Asya](https://forum.graphviz.org/u/Asya)\
**Post date:** [March 15, 2022, 10:02am UTC](https://forum.graphviz.org/t/how-can-i-draw-undirected-graph/1069/9 "2022-03-15T10:02:01Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![steveroush](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@steveroush](https://forum.graphviz.org/u/steveroush)\
**Post date:** [March 15, 2022, 2:43pm UTC](https://forum.graphviz.org/t/how-can-i-draw-undirected-graph/1069/10 "2022-03-15T14:43:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![smattr](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/smattr/32/85_2.png) [@smattr](https://forum.graphviz.org/u/smattr)\
**Post date:** [March 15, 2022, 2:51pm UTC](https://forum.graphviz.org/t/how-can-i-draw-undirected-graph/1069/11 "2022-03-15T14:51:19Z")

</div>

> [@Asya](#):
>
> do you by any chance know any other python packages that can manage to draw huge graphs?

This one isn’t Graphviz based, but [networkx](https://networkx.org/) tries to hit this niche.

---

<div class="post-metadata">

**Author:** ![Asya](https://avatars.discourse-cdn.com/v4/letter/a/22d042/32.png) [@Asya](https://forum.graphviz.org/u/Asya)\
**Post date:** [March 15, 2022, 3:38pm UTC](https://forum.graphviz.org/t/how-can-i-draw-undirected-graph/1069/12 "2022-03-15T15:38:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Asya](https://avatars.discourse-cdn.com/v4/letter/a/22d042/32.png) [@Asya](https://forum.graphviz.org/u/Asya)\
**Post date:** [March 15, 2022, 3:39pm UTC](https://forum.graphviz.org/t/how-can-i-draw-undirected-graph/1069/13 "2022-03-15T15:39:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![steveroush](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@steveroush](https://forum.graphviz.org/u/steveroush)\
**Post date:** [March 15, 2022, 4:09pm UTC](https://forum.graphviz.org/t/how-can-i-draw-undirected-graph/1069/14 "2022-03-15T16:09:33Z")

</div>

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](https://graphviz.org/docs/layouts/sfdp/)) 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](https://graphviz.org/doc/info/command.html#-v)) to get debugging/logging info
- look here ([https://graphviz.org/doc/info/attrs.html\_](https://graphviz.org/doc/info/attrs.html_)) for attributes that might speed-up or improve your **sfdp** runs.

---

<div class="post-metadata">

**Author:** ![Asya](https://avatars.discourse-cdn.com/v4/letter/a/22d042/32.png) [@Asya](https://forum.graphviz.org/u/Asya)\
**Post date:** [March 15, 2022, 4:29pm UTC](https://forum.graphviz.org/t/how-can-i-draw-undirected-graph/1069/15 "2022-03-15T16:29:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![scnorth](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/scnorth/32/89_2.png) [@scnorth](https://forum.graphviz.org/u/scnorth)\
**Post date:** [March 15, 2022, 11:38pm UTC](https://forum.graphviz.org/t/how-can-i-draw-undirected-graph/1069/16 "2022-03-15T23:38:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Asya](https://avatars.discourse-cdn.com/v4/letter/a/22d042/32.png) [@Asya](https://forum.graphviz.org/u/Asya)\
**Post date:** [March 17, 2022, 10:12am UTC](https://forum.graphviz.org/t/how-can-i-draw-undirected-graph/1069/17 "2022-03-17T10:12:57Z")

</div>

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
