如何在networkx中更漂亮地绘制一棵树 [英] How to draw a tree more beautifully in networkx
问题描述
如果我使用networkx制作一棵树并将其绘制,则节点重叠.有没有办法画出来所以没有重叠?
将matplotlib.pyplot导入为plt将networkx导入为nxT = nx.generators.balanced_tree(2,5)nx.draw(T)plt.show()
我不是这方面的专家,但这里的代码使用了 pydot
库及其 graph_viz
依赖性.这些库随 Anaconda Python 一起提供,但默认情况下未安装,因此请先从命令提示符执行此操作:
conda 安装 pydot
然后,这里的代码改编自
或者,如果您更喜欢自顶向下的树,您可以将代码中的字符串 "twopi"
替换为 "dot"
,并且如果您使结果窗口变宽
此外,如果您使用字符串"circo"
并扩大窗口范围,则会得到
If I make a tree using networkx and draw it, the nodes overlap. Is there a way to draw it so there is no overlap?
import matplotlib.pyplot as plt
import networkx as nx
T = nx.generators.balanced_tree(2, 5)
nx.draw(T)
plt.show()
I am no expert in this, but here is code that uses the pydot
library and its graph_viz
dependency. These libraries come with Anaconda Python but are not installed by default, so first do this from the command prompt:
conda install pydot
Then here is code adapted from Circular Tree.
import matplotlib.pyplot as plt
import networkx as nx
import pydot
from networkx.drawing.nx_pydot import graphviz_layout
T = nx.balanced_tree(2, 5)
pos = graphviz_layout(T, prog="twopi")
nx.draw(T, pos)
plt.show()
If you adjust the window to make it square, the result is
Or, if you prefer a top-down tree, you could replace the string "twopi"
in that code with "dot"
, and if you make the resulting window wider you get
Also, if you use the string "circo"
instead and make the window wider, you get
这篇关于如何在networkx中更漂亮地绘制一棵树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!