简体   繁体   English

如何在 python 中绘制有序树?

[英]How to draw an ordered tree in python?

I have an XML file whose underlying structure is an ordered tree.我有一个 XML 文件,其基础结构是有序树。 I use digraph in networks representing this tree and then I want to draw this tree.我在表示这棵树的网络中使用有向图,然后我想画这棵树。 Suppose the digraph is G, then I write the following code:假设有向图是G,那么我编写如下代码:

map = dict(zip(id,tag)) # map from id to label
pos = nx.pydot_layout(G,prog = 'dot')
labels = nx.draw_networkx_labels(G, pos, map) 
nx.draw_networkx(G, pos, False, node_size = 1000, node_color = color)
plt.show()

but i cannot get a ordered tree.但我无法得到有序的树。 the order of silbing nodes are not in their original order. silbing节点的顺序不是它们原来的顺序。

I want to know how can I plot an ordered tree in python, thanks,我想知道我怎样才能 plot python 中的有序树,谢谢,

Don't call a variable map , there is a builtin called map .不要调用变量map ,有一个名为map的内置函数。

You can use an OrderedDict to keep the items in order:您可以使用OrderedDict来保持项目有序:

from collections import OrderedDict
from itertools import izip
themap = OrderedDict(izip(id,tag)) # map from id to label

You can also get it from PyPI if you have an older version of Python than 2.7 / 3.2.如果您的 Python 版本比 2.7 / 3.2 旧,您也可以从 PyPI 获取它

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

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