如何在`ete3`树上留下颜色? (Python 3)

时间:2016-09-08 00:34:50

标签: python colors tree etetoolkit ete3

我刚刚开始使用ete3而且非常棒。

如何使用颜色字典为ete3树对象的叶子着色?我制作"c":None因为我不想要{{1} }} 现身。

我想更好地控制树渲染,但我无法弄清楚如何做到这一点。

我看到有NodeStyle objects,但我认为这是针对实际节点的。看起来TextFace object是我需要的,但我不知道如何使用它。 All the examples正在添加标签。

c

enter image description here

我看了这个问题但是很混乱: How to color tree nodes with fixed set of colors?

1 个答案:

答案 0 :(得分:3)

我会这样做:

from ete3 import Tree, TextFace, TreeStyle

# Build Tree
tree = Tree( "((a,b),c);" )
# Leaf mapping
D_leaf_color = {"a":"red", "b":"green"}
for node in tree.traverse():
    # Hide node circles
    node.img_style['size'] = 0
    if node.is_leaf():
        color = D_leaf_color.get(node.name, None)
        if color:
            name_face = TextFace(node.name, fgcolor=color, fsize=10)
            node.add_face(name_face, column=0, position='branch-right')
# Set up style for circular tree
ts = TreeStyle()
ts.mode = "c"
ts.scale = 10
# Disable the default tip names config
ts.show_leaf_name = False
ts.show_scale = False
# Draw Tree
tree.render("tree_test.png", dpi=300, w=500, tree_style=ts)

enter image description here