4

My question is in Pydot how can I set the node order in a cluster?

My code is

import pydot
graph = pydot.Dot(graph_type='digraph', rankdir="LR")
graphlegend = pydot.Cluster(graph_name="legend", label="Legend", fontsize="15", color="red", style="filled", fillcolor="lightgrey")

legend1 = pydot.Node("Sample", style="filled", fillcolor="Tomato", shape="diamond", rank="same"); graphlegend.add_node(legend1)

legend2 = pydot.Node('a', style="filled", fillcolor="LightGoldenrod", shape="Mrecord", label="Protein", rank="same"); graphlegend.add_node(legend2)

node_c = pydot.Node("ff", style="filled", fillcolor="#9ACEEB", shape="square"); graph.add_node(node_c)

graph.write_png('Sample_diagraph.png')

I would like to have the cluster "legend" and the "node_c" in vertical order but the 2 nodes (legend1 and legend2) in the "graphlegend" cluster organized horizontally in line. I have tried to use rank=same but doesn't work Can you help me please?

Jérémz
  • 383
  • 1
  • 5
  • 20

2 Answers2

4

I finally found an alternative solution by using an invisible edge

the code is now:

graph = pydot.Dot(graph_type='digraph', rankdir="LR")

graphlegend = pydot.Cluster(graph_name="legend", label="Legend", fontsize="15", color="red", style="filled", fillcolor="lightgrey", rankdir="TB")
legend1 = pydot.Node("Sample", style="filled", fillcolor="Tomato", shape="diamond", rank="same"); graphlegend.add_node(legend1)
legend2 = pydot.Node('a', style="filled", fillcolor="LightGoldenrod", shape="Mrecord", label="Protein", rank="same"); graphlegend.add_node(legend2)

graph.add_subgraph(graphlegend)

graph.add_edge(pydot.Edge(legend1, legend2, style="invis"))

By doing so my different Nodes in the graph are still organized in vertical and the rank in horizontal. And my independent Cluster is now showing the nodes in horizontal thanks to the invisible Edge.

enter image description here

Jérémz
  • 383
  • 1
  • 5
  • 20
1

Most probably you want rankdir="TB" top to bottom ranking. Vertical ordering is done then by rank depending on visible or invisible edges. rank="same" may be needed if there are horizontal edges.

stefan
  • 3,681
  • 15
  • 25
  • When i'm replacing rankdir="LR" by rankdir="TB" it does make everything horizontal but not only in the cluster the node_c also is on the right of the cluster. – Jérémz Mar 13 '15 at 00:18