7

I have been building a graph via Python using the Networkx library only. My program outputs a .dot file with nx.drawing.nx_pydot.to_pydot() and uses the dot engine to draw that file with Graphviz, by calling write_png("output.png") on the dot object.

My dot file could look like this:

strict digraph  {
a;
b;
c;
a -> b;
a -> c;
}

I want to add the dot file to be as follows:

strict digraph  {
a;
b;
c;
a -> b;
a -> c;
{rank=same; a b};
}

Because in the previous case b and c are drawn below a and in the later case c is drawn below a and b, which are at the same level.

I cannot find, in the documentation, how to make this simple change. I understand this is the same as declaring a subgraph and setting its attribute, but the part I am struggling with is setting the rank attribute of that subgraph.

After my graph has been constructed, the following code handles the output.

DG = myGraphBuildFunction(dictOfEdges)
p = nx.drawing.nx_pydot.to_pydot(DG)
p.write_png('example.png')
nx.drawing.nx_pydot.write_dot(DG, "Output.dot")
print("Graph Generated")

How can I easily set the rank of some nodes from here? I want to avoid the pygraphviz library, but I am fine with the graphviz library. Pygraphviz won't easily install on Windows via pip install and I need to keep this program "easy to use and maintain", as I am not the only one who will be potentially using it.

ZoSal
  • 561
  • 4
  • 21
  • see answer in https://stackoverflow.com/questions/22207802/pygraphviz-networkx-set-node-level-or-layer – yehudahs Apr 22 '19 at 04:36
  • thank you @yehudahs , however it's stated in both the question title and in the final paragraph that the requested solution should only use networkx and not involve Pygraphviz. – ZoSal Apr 23 '19 at 12:48

0 Answers0