# Retaining hierarchy of graph in cluster

**URL:** <https://forum.graphviz.org/t/retaining-hierarchy-of-graph-in-cluster/2150>\
**Category:** Uncategorized\
**Created:** [April 24, 2024, 2:34am UTC](https://forum.graphviz.org/t/retaining-hierarchy-of-graph-in-cluster/2150 "2024-04-24T02:34:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nwl](https://avatars.discourse-cdn.com/v4/letter/n/45deac/32.png) [@nwl](https://forum.graphviz.org/u/nwl)\
**Post date:** [April 24, 2024, 2:34am UTC](https://forum.graphviz.org/t/retaining-hierarchy-of-graph-in-cluster/2150/1 "2024-04-24T02:34:34Z")

</div>

Hi all,

 ![Screenshot 2024-04-24 100922](https://global.discourse-cdn.com/graphviz/original/2X/7/704780601450febd56a06db2d76f96f08745abed.png)

I am currently using the Python API. Above is my current graph.

The black nodes shown below are the original nodes. However, after adding the red nodes as shown below, it resulted in the graph above. In other words, how can I add the red nodes shown in the graph below while retaining the hierarchy in the second cluster by aligning the nodes to the top within the second layer?

 ![Screenshot 2024-04-24 101938](https://global.discourse-cdn.com/graphviz/original/2X/3/3246d0752f1ce3cc14a335d24d43829972bdebd3.png)

 ![Screenshot 2024-04-24 100922-1](https://global.discourse-cdn.com/graphviz/original/2X/8/8a033637db40dc6c7dcfb7c04a8f9c723e637759.png)

Thanks.

---

<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:** [April 24, 2024, 3:47am UTC](https://forum.graphviz.org/t/retaining-hierarchy-of-graph-in-cluster/2150/2 "2024-04-24T03:47:46Z")

</div>

It is very difficult to debug/change a program without access to the source.  
That said, I will take a _wild_ guess at what you want to do - look at the **TBbalance** attribute ([TBbalance | Graphviz](https://graphviz.org/docs/attrs/TBbalance/))  
If that does not solve your problem, please provide source. If you can’t share proprietary information, we can provide an anonymizer program.

---

<div class="post-metadata">

**Author:** ![nwl](https://avatars.discourse-cdn.com/v4/letter/n/45deac/32.png) [@nwl](https://forum.graphviz.org/u/nwl)\
**Post date:** [April 24, 2024, 11:13am UTC](https://forum.graphviz.org/t/retaining-hierarchy-of-graph-in-cluster/2150/5 "2024-04-24T11:13:06Z")

</div>

Hi @steveroush,

I have mainly resolved my issue by introducing nested subgraphs within each subgraph cluster for each layer.

However, am now facing this issue instead…

 ![Screenshot 2024-04-24 190624](https://global.discourse-cdn.com/graphviz/original/2X/7/7d7323d02385378977e4a71502372c5ff052fa5f.png)

Would like to ask if you have any suggestions to resolve the issue above?

Am currently using Python API with html like labels.  
[https://graphviz.readthedocs.io/en/stable/api.html](https://graphviz.readthedocs.io/en/stable/api.html)

Thank you for the help.

---

<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:** [April 24, 2024, 3:07pm UTC](https://forum.graphviz.org/t/retaining-hierarchy-of-graph-in-cluster/2150/6 "2024-04-24T15:07:32Z")

</div>

Is this a one-time graph (quick & dirty solution is probably OK) or a run-many-times graph (really want to automate completely?)

p.s. if you want to try creating an anonymous version to share, here is a link to an anonymizer GVPR program ([cmd/gvpr/lib/anon · main · graphviz / graphviz · GitLab](https://gitlab.com/graphviz/graphviz/-/blob/main/cmd/gvpr/lib/anon))  
download it, produce a _dot_ format output from your Python program (names myFile.gv) and use this command line `gvpr -f anon myFile.gv`. Note that you may need to hand-edit the cluster names.  
Link to GVPR - part of the Graphviz package -([https://www.graphviz.org/pdf/gvpr.1.pdf](https://www.graphviz.org/pdf/gvpr.1.pdf))

---

<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:** [April 25, 2024, 3:44am UTC](https://forum.graphviz.org/t/retaining-hierarchy-of-graph-in-cluster/2150/7 "2024-04-25T03:44:19Z")

</div>

Just making guesses about the input …  
There are 3 edges from nodes in the next-to-lowest rank (row) of the middle cluster to nodes in the top rank of the bottom cluster. Add **minlen=2** to these edges ([minlen | Graphviz](https://graphviz.org/docs/attrs/minlen/)). That _should_ push the bottom cluster down, and (with luck) then underneath the middle cluster.

---

<div class="post-metadata">

**Author:** ![nwl](https://avatars.discourse-cdn.com/v4/letter/n/45deac/32.png) [@nwl](https://forum.graphviz.org/u/nwl)\
**Post date:** [April 26, 2024, 9:53am UTC](https://forum.graphviz.org/t/retaining-hierarchy-of-graph-in-cluster/2150/8 "2024-04-26T09:53:33Z")

</div>

Hi @ steveroush,

You were right, the issue was resolved after I added the correct minlen!

Thank you for the help.

---

<div class="post-metadata">

**Author:** ![BartB](https://avatars.discourse-cdn.com/v4/letter/b/848f3c/32.png) [@BartB](https://forum.graphviz.org/u/BartB)\
**Post date:** [April 26, 2024, 2:22pm UTC](https://forum.graphviz.org/t/retaining-hierarchy-of-graph-in-cluster/2150/9 "2024-04-26T14:22:24Z")

</div>

You can also control the layout with osage layout and use “packmode” as explained here: [Is there a way to draw “desks” (2 boxes side by side and 2 below them) without using arrows on graphviz? - Help - Graphviz](https://forum.graphviz.org/t/is-there-a-way-to-draw-desks-2-boxes-side-by-side-and-2-below-them-without-using-arrows-on-graphviz/1987)
