[英]How to color leaves on `ete3` Tree? (Python 3)
I just started using ete3
and it is awesome. 我刚开始使用
ete3
,它很棒。
How can I color the leaves of an ete3
Tree object using a color dictionary? 如何使用颜色字典为
ete3
树对象的叶子着色? I made "c":None
because I don't want the of c
to show up. 我做了
"c":None
因为我不希望c
出现。
I want to have better control of the tree render but I can't figure out exactly how to do it. 我想更好地控制树的渲染,但是我不知道该怎么做。
I saw that there are NodeStyle
objects but I think this is for the actual nodes. 我看到有
NodeStyle
对象,但我认为这是针对实际节点的。 It looks like this TextFace
object is what I need but I don't know how to use it. 看起来这个
TextFace
对象是我所需要的,但我不知道如何使用它。 All the examples are adding labels. 所有示例都在添加标签。
# 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)
I looked at this question but it was pretty confusing: How to color tree nodes with fixed set of colors? 我看着这个问题,但是却很困惑: 如何用固定的颜色为树节点着色?
I would do it like this: 我会这样做:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.