简体   繁体   中英

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

从片段生成的图像

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM