python - 如何使用 ETE Toolkit (Python) 更改节点标签上的字体方向

标签 python tree visualization hierarchical-data etetoolkit

我想更改 ETE 工具包生成的输出图像上的字体方向:http://etetoolkit.org

由于某些原因,旋转和方向变化不会影响标签,如下图所示:

ETE

在 Jupiter notebook 上生成此示例的代码如下:

from ete3 import Tree, TreeStyle

def draw_ete(newick):
    t = Tree(newick)
    ts = TreeStyle()
    ts.scale = 5
    ts.rotation = -90
    ts.orientation = 1
    return t.render("%%inline", tree_style=ts)

newick = """((p, e, a, s, ((e, _), p, o), r, r, i, d, g, (e, _)), (h, o, t), ((,, ↵), (p, e, a, s, ((e, _), p, o), r, r, i, d, g, (e, _))), (c, (o, l, d)), ((,, ↵), (p, e, a, s, ((e, _), p, o), r, r, i, d, g, (e, _))), ((i, n), _, t, h, ((e, _), p, o), t, (,, ↵), n, (i, n), (e, _), d, a, y, s, _, (o, l, d), .), ↵, ↵, (s, o, m, (e, _), l, i, k, (e, _), i, t, _), (h, o, t), ((,, ↵), (s, o, m, (e, _), l, i, k, (e, _), i, t, _)), (c, (o, l, d)), ((,, ↵), (s, o, m, (e, _), l, i, k, (e, _), i, t, _)), ((i, n), _, t, h, ((e, _), p, o), t, (,, ↵), n, (i, n), (e, _), d, a, y, s, _, (o, l, d), .));"""
draw_ete(newick)

我还想知道是否可以将输出拆分为多行而不是一行?长序列往往会占用巨大的宽度空间,因此将序列拆分为多行是可行的。

最佳答案

来自 etetoolkit 邮件列表的回答:

1) 可以使用自定义布局和 TextFaces 进行旋转。 检查这个例子:

from ete3 import Tree, TreeStyle, add_face_to_node, TextFace

from random import randint

def rotation_layout(node):
    if node.is_leaf():
        F = TextFace(node.name, tight_text=True)
        F.rotation = randint(0, 360)
        add_face_to_node(TextFace("third" ), node, column=8, position="branch-right")
        add_face_to_node(TextFace("second" ), node, column=2, position="branch-right")
        add_face_to_node(F, node, column=0, position="branch-right")

        F.border.width = 1
        F.inner_border.width = 1

def get_example_tree():
    t = Tree()
    t.populate(10)
    ts = TreeStyle()
    ts.rotation = 45
    ts.show_leaf_name = False
    ts.layout_fn = rotation_layout

    return t, ts

if __name__ == "__main__":
    t, ts = get_example_tree()
    t.show(tree_style=ts)

rotated tree labels

2) 分成几行:我不完全理解你的问题,但是,如果询问减少最终 TreeMap 像的宽度,唯一的方法是将绘图分成多个渲染调用,每个节点一个你想画画。 例如。要将一棵树分成两个独立的图像,您可以运行:

t.children[0].render("left_side.png")
t.children[1].render("right_side.png")

3) 你是说底部的分支比例? 你可以设置TreeStyle.show_scale=False

关于python - 如何使用 ETE Toolkit (Python) 更改节点标签上的字体方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35551004/

相关文章:

css - 如何在 Google Visualization API 中指定自定义 CSS 字体/字体系列?

python - 使用 PIL 的 ImageDraw 模块

python - 如何将数据框转换为csv?

python - PyCharm 项目文件消失了

algorithm - 线段树、区间树、二进制索引树和范围树之间有什么区别?

r - gganimate 循环之间的暂停

python - 如何将数据过滤器添加到我的谷歌表格标题

python - 绘制嵌套的 networkx 图

python - 从树数据结构中提取所有链

python - 如何减慢 Pillow 中的图像显示速度