简体   繁体   English

如何使用 pygraphviz 增加点图中节点之间的间距?

[英]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.我希望在此图上放置标签,因此节点之间的间距必须相当大,但我不确定 pygraphvis_layout 的“args”参数如何工作,或者我是否正确使用它。

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.似乎相同等级的节点之间的间距应该至少为 2 英寸,但这并没有反映在实际图像中。 Varying the number provided to nodesep has no effect on spacing as far as I've tested.据我测试,改变提供给 nodeep 的数字对间距没有影响。

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.我已经查看了其他解决方案: pydot 没有工作,似乎 output png 文件我无法打开,我已经在使用 NetworkX 来绘制其他相关内容。

(Replication will require graphviz in addition to the indicated imports.) (除了指定的导入之外,复制还需要 graphviz。)

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.一旦大小足够大, Gnodesep参数应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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