Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I increase spacing between nodes in dot graph with pygraphviz?

I'm having trouble trying to increase the spacing between nodes in a hierarchical graph I'm making. I expect to put labels on this graph, so the spacing between nodes has to be fairly large, but I'm unsure how the "args" parameter of pygraphvis_layout works, or if I'm using it properly.

It seems as if the spacing between nodes of the same rank should be at least 2 inches, but this doesn't reflect in the actual images. Varying the number provided to nodesep has no effect on spacing as far as I've tested.

I've already looked over other solutions: pydot hasn't worked and seems to output png files I can't open, and I already am using NetworkX for graphing something else related.

(Replication will require graphviz in addition to the indicated imports.)

import networkx as nx
import matplotlib.pyplot as plt
from networkx.drawing.nx_agraph import pygraphviz_layout

plt.figure(figsize=(10, 7))
graph = nx.DiGraph([(0, 1), (0, 2),
                    (1, 3), (1,4), (2,5), (2,6),
                    (3, 7), (3, 8), (4, 9), (4, 10), (5, 11), (5, 12), (6, 13), (6, 14)])
pos = pygraphviz_layout(graph, prog="dot", args='-Gnodesep=2')
nx.draw_networkx_nodes(
    graph, pos, nodelist=graph.nodes, node_size=1000, node_color="r", alpha=0.8
)
nx.draw_networkx_edges(graph, pos, edgelist=graph.edges, width=1, edge_color="k")
plt.axis("off")
plt.savefig("test.svg")
plt.show()

Resulting image from snippet

like image 266
aleph-null Avatar asked Aug 17 '20 01:08

aleph-null


1 Answers

The space for plotting the graph is limited by the plot's figure size. Try to play with the size, for example:

pos = pygraphviz_layout(graph, prog="dot", args='-Gnodesep=2')
nx.draw_networkx_nodes(...)
nx.draw_networkx_edges(...)
plt.figure(figsize=(20,20))
plt.show()

Once the size is large enough the Gnodesep argument should work.

like image 176
zohar.kom Avatar answered Oct 12 '22 13:10

zohar.kom