如何给“ete3”树上的叶子上色? (Python 3)

How to color leaves on `ete3` Tree? (Python 3)

我刚开始使用 ete3,它很棒。

如何使用颜色字典为 ete3 Tree 对象的叶子着色? 我做了 "c":None 因为我不想要的c 出现。

我想更好地控制树的渲染,但我不知道该怎么做。

我看到有 NodeStyle objects but I think this is for the actual nodes. It looks like this TextFace object is what I need but I don't know how to use it. All the examples 正在添加标签。

# Build Tree
tree = ete3.Tree( "((a,b),c);" )
# Leaf mapping
D_leaf_color = {"a":"r", "b":"g","c":None}
# Set up style for circular tree
ts = ete3.TreeStyle()
ts.mode = "c"
# Draw Tree
tree.render("tree_test.png", dpi=300, w=500, tree_style=ts)

我看了这个问题,但它很混乱:

我会这样做:

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)