How to draw an ordered tree in python?
I have an XML file whose underlying structure is an ordered tree. 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:
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 = 开发者_Go百科1000, node_color = color)
plt.show()
but i cannot get a ordered tree. the order of silbing nodes are not in their original order.
I want to know how can I plot an ordered tree in python, thanks,
Don't call a variable map
, there is a builtin called map
.
You can use an OrderedDict to keep the items in order:
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.
精彩评论